This forum is now read-only. Please use our new forums at

While Loops in PHP Forum

View Course » View Exercise

866 points
Submitted by
Cyrus Vence
over 1 year ago

Step by Step explanation of "Looping the Loop" exercise

Tried to simplify the explanation

$headCount = 0; // setting the variable
$flipCount = 0; // setting the variable
while ($headCount < 3) { // while variable is less than 3, do the following until it gets 3 flips SUCCESSIVELY [in sequence]. When it gets 2 flips and the next one is false [0], it starts again from the beginning. if you want you can experiment with the number 3. just put for example, number 9 and see what happens.
$flip = rand(0,1);
$flipCount ++; // this variable is responsible to count those 3 flips in sequence
if ($flip){ // if the flip [$flip = rand(0,1);] is true
$headCount ++; // this increments +1 (if the above is true and prints echo statement to the screen under the letter "H". if it false, it prints letter "T"
echo "H";
else {
$headCount = 0;
// this one here is responsible to get those "H" letter in a row. if we remove it, it gets 3 "H" and stops not paying attention to if the letter "H" comes in a row (in sequence)
echo "T";
echo "It took {$flipCount} flips!";