Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
php-while-loops/index.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
108 lines (107 sloc)
2.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Coin Flips</title> | |
</head> | |
<body> | |
<p>We are going to flip a coin until we get three heads in a row!</p> | |
<?php | |
$headCount = 0; | |
$flipCount = 0; | |
while ($headCount < 3) { | |
$flip = rand(0,1); | |
$flipCount ++; | |
if ($flip){ | |
$headCount ++; | |
echo "<div class=\"coin\">H</div>"; | |
} | |
else { | |
$headCount = 0; | |
echo "<div class=\"coin\">T</div>"; | |
} | |
} | |
echo "<p>It took {$flipCount} flips!</p>"; | |
include 'index.html'; | |
?> | |
<?php | |
$loopCond = true; | |
while ($loopCond == true){ | |
//Echo your message that the loop is running below | |
echo "<p>The loop is running.</p>"; | |
$loopCond = false; | |
} | |
echo "<p>And now it's done.</p>"; | |
?> | |
<?php | |
//Add while loop below | |
$loopCount = 0; | |
while ($loopCount < 4): | |
echo "<p>Iteration number: {$loopCount} </p>"; | |
$loopCount ++; | |
endwhile; | |
?> | |
<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>"; | |
?> | |
<p>We will keep rolling this die until we get a six!</p> | |
<?php | |
$rollCount = 0; | |
do { | |
$roll = rand(1,6); | |
switch ($roll): | |
case 1: | |
echo "You rolled a 1, try again <br>"; | |
$rollCount ++; | |
break; | |
case 2: | |
echo "You rolled a 2, try again <br>"; | |
$rollCount ++; | |
break; | |
case 3: | |
echo "You rolled a 3, try again <br>"; | |
$rollCount ++; | |
break; | |
case 4: | |
echo "You rolled a 4, keep trying <br>"; | |
$rollCount ++; | |
break; | |
case 5: | |
echo "You rolled a 5, almost try again <br>"; | |
$rollCount ++; | |
break; | |
case 6: | |
echo "You rolled a 6" ; | |
$rollCount ++; | |
break; | |
default: | |
echo "No result! The dice rolled off the table"; | |
endswitch; | |
} while ($roll !=6); | |
echo ", and it took you $rollCount tries<br>"; | |
?> | |
<?php | |
$i = 0; | |
do { | |
echo $i; | |
} while ($i > 0); | |
?> | |
</body> | |
</html> |