Search the Community
Showing results for tags 'codecourse'.
-
Hello I am learning PHP and made a codeacademy PHP online course. Basic stuff and interesting course for free. But there is a do/while exercise I can't get my head around: In the following, there is a flipcoin example, which flips, as long the result is head. In the do statement there is the if and the else The if condition is $flip and it echoes a H letter for Head. The else echoes a T for Tail. $flip is random 0 or 1 My question: How can the if and else statements be H or T if $flip is random? I don't understand the $flip variable, because in my head it can be 0 or 1.....therefor if is not one fixed value, because of the rand(0,1) it can change How does the programm to put out a H or a T? I hope my question is clear enough Thanks for any clarification Jakob <!DOCTYPE html> <html> <head> <link type='text/css' rel='stylesheet' href='style.css'/> <title>More Coin Flips</title> </head> <body> <p>We will keep flipping a coin as long as the result is heads!</p> <?php $flipCount = 0; do { $flip = rand(0,1); $flipCount ++; if ($flip){ echo "<div class=\"coin\">H</div>"; } else { echo "<div class=\"coin\">T</div>"; } } while ($flip); $verb = "were"; $last = "flips"; if ($flipCount == 1) { $verb = "was"; $last = "flip"; } echo "<p>There {$verb} {$flipCount} {$last}!</p>"; ?> </body> </html>