I am trying to get a program to reiterate until both the $heads variable and $tails variable is greater than 0. I can’t figure out what I am doing wrong. The while loop always gets broken after just one iteration.
<?php
echo "<table border=\"1\">";
echo "<tr><td>Person</td><td>Heads</td><td>Tails</td><td>Total</td></tr>";
for ($person=1; $person < 11; $person++){
echo "<tr><td>Person $person </td>";
$both = 0;
$heads = 0;
$tails = 0;
$total = 0;
while ($both < 1){
do {
$total++;
$random = rand(1,2);
if ($random == 1){
$heads++;
} else{
$tails++;
}
} while (($tails < 0) && ($heads < 0));
$both = 1;
}
echo "<td>$heads</td><td>$tails</td><td>$total</td>";
echo "</tr>";
}
echo "</table>";
?>
In this line
it appears that neither
$tailsnor$headswill ever be strictly less than0, so that will always be false. Try<= 0for both.Also, logically, you want to loop again if either of those conditions is true, right? So use
||instead of&&.Result:
Also, I’m a little curious about the
while ($both < 1)loop. It appears that you assign$both = 0before the loop, and you assign$both = 1at the end of its iteration. That would guarantee the loop is only executed once, in which case — what’s the point of having that loop? Perhaps that’s just unfinished code at this point?