PHP 6 sided die counting number of rolls


I'm very new to PHP and I am stuck. I'm trying an exercise where I am rolling a 6 sided die and counting the number of times it is rolled until there is X number of 1s rolled in a row. The code I have up until this point is:

<!DOCTYPE html>
    <link type='text/css' rel='stylesheet' href='style.css'/>
    //Practiceing while and do while loops
$onecount = 0
$rollcount = 0
while ($onecount < 7) {
    $roll = rand(1,6);
    $rollcount ++;
    if ($roll!=1) {
        echo {$rollcount};
    else {
        $onecount ++;


I feel like I'm missing something very simple but I didn't know what else to do.

Thank you in advance!




You need to reset your$onecount variable when the role isn't 1, since the streak is broken each time it's a different number. Also, you probably want to do something after the while loops finishes and you have your total roll count.

People are also looking for solutions to the problem: mysql - PHP Limit with session var sql error


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.