aurimasju Posted November 8, 2017 Share Posted November 8, 2017 Hi, I'm new to php so I'm trying to code some random stuff.. <?php $headCount = 0; $loopCount = 0; do { $flip = rand(0,1); if($flip = 0) { echo "<p>H</p>"; $loopCount++; $headCount++; } else { echo "<p>T</p>"; $loopCount++; $headCount = 0; } } while($headCount == 2 or $loopCount == 5); //should flip it til loopcount reach 5 or headcount reach 2 if($loopCount <= 5 && $headCount == 2) { echo "<p>We rolled " . $headCount . " heads in a row in " . $loopCount . " flips.</p>"; } else { echo "<p> " . $loopCount . " roll(s) was not enough to get two <b>H</b> in a row.</p>"; } ?> but all it does is in a screenshot .. thanks! Link to comment Share on other sites More sharing options...
Sepodati Posted November 8, 2017 Share Posted November 8, 2017 Your logic is flawed. The loop will only continue if the conditions evaluate to true. $headcount is not equal to 2 and $loopcount is not equal to 5, so the condition fails and the loop exists after the first go. What I think you want is the loop to continue while $headcount is less than 2 or $loopcount is less than 5. Link to comment Share on other sites More sharing options...
requinix Posted November 8, 2017 Share Posted November 8, 2017 To answer the next question you'll have (if you don't answer it yourself), take a good look at the if condition in the loop. Link to comment Share on other sites More sharing options...
aurimasju Posted November 9, 2017 Author Share Posted November 9, 2017 Thank you guys! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.