- rand: documentation ( source)
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
//Add while loop below
echo "<p>We will flip until we have heads.</p>";
$flipCount = 1;
$headCount = 0;
$tailCount = 0;
while($headCount or $tailCount < 3) {
$num = rand(0,1);
if($num > 0.5) {
echo "Heads. ";
$headCount ++;
$tailCount = 0;
}
else {
echo "Tails. ";
$headCount = 0;
$tailCount ++;
}
if($headCount == 3) {
echo "Congratulations! You got 3 heads in a row! It took you $flipCount tries.";
}
elseif($tailCount == 3) {
echo "Congratulations! You got 3 tails in a row! It took you $flipCount tries.";
}
else {
echo "No luck yet. You haven't gotten it after $flipCount tries.<br />";
}
$flipCount ++;
}
?>
</body>
</html>