简体   繁体   中英

Learning PHP and having do/while issue

I am currently trying to learn PHP and am stuck on a do/while with else/if loop and am totally baffled. Would appreciate if anybody could let me know where i am going wrong with this.

<?php
    $rollCount = 0;
    $sixCount = false;
    do {
        echo "<p>So you want to roll a 6 ? </p>";
    } while($sixCount == false);
        $roll = rand(1,6);
        $rollCount ++;
        if($roll != 6) {
            $sixCount = false;
            echo "<p>You just rolled a " .$roll. " .</p>";
        }
        else {
            $sixCount = true;
            echo "<p>Nice, you just rolled a 6</p>";
        }


    echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";
    ?>

I think i may have written an infinite loop, as the preview screen i am working on just shows loading animation.

My little mind tries to look at it like this.

While $sixCount is equal to false, $roll a random number between 1 and 6 and add a +1 to roll count.

IF the $roll does not = to 6, than the $sixCount is false and print "you just rolled a $roll" or else $sixCount is true and you just rolled a 6.

I try to simplify it in my mind by putting into a sentence and transferring it into PHP. Is this the wrong way to look at PHP ?

Appreciate all help on this. Thanks!!

I think you're misunderstanding the concept of the do...while loop. You need to put all your conditions and output into the do part, and the while part simply defines the condition that should allow the do set to keep looping.

Try shifting your code under the while line into the do statement:

$rollCount = 0;
$sixCount = false;

echo "<p>So you want to roll a 6 ? </p>";

do {
    $roll = rand(1,6);
    $rollCount++;
    if($roll != 6) {
        $sixCount = false; // this line is unnecessary 
        echo "<p>You just rolled a {$roll}.</p>";
    }
    else {
        $sixCount = true;
        echo "<p>Nice, you just rolled a 6</p>";
    }
} while($sixCount == false);

echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";

An alternative, using the while construct without do would possibly be more applicable to the way you've laid out your code:

$rollCount = 0;
$sixCount = false;

echo "<p>So you want to roll a 6 ? </p>";

while ($sixCount == false) {
    $roll = rand(1,6);
    $rollCount++;
    if($roll != 6) {
        $sixCount = false; // this line is unnecessary
        echo "<p>You just rolled a {$roll}.</p>";
    }
    else {
        $sixCount = true;
        echo "<p>Nice, you just rolled a 6</p>";
    }
}

echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";

It appears that you are writing the code you want to continuously execute until it rolls a 6 is outside of the while loop, this is what the code should look like.

<?php
$rollCount = 0;
$sixCount = false;
echo "<p>So you want to roll a 6 ? </p>";
do{
    $roll = rand(1,6);
    $rollCount ++;
    if($roll != 6) {
        $sixCount = false;
        echo "<p>You just rolled a " .$roll. " .</p>";
    }
    else {
        $sixCount = true;
    }
}while($sixCount == false);

echo "<p>Nice, you just rolled a 6</p>";
echo "<p>It took {$rollCount} rolls to land 6... What are the odds!</p>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM