KillZoneZ Posted August 24, 2015 Share Posted August 24, 2015 (edited) So, today i decided i would start learning PHP, i just learned how to do while loops and i wanted to get a bit out of the comfort zone of what the tutorials i've seen gave me. So i decided to create a quick "interaction" where the player receives a random amount of damage from a monster, in this case a zombie, and then displays the damage received and the current health after the damage has been taken. This is what i have: <?php $LostHealth = rand(1,10); $Health = 100; if ($Health == 0) { echo "You are dead."; } else { echo "You turn around and there is a zombie coming in your direction"; while ($Health > 0) { $Health = $Health - $LostHealth; echo "<p>A Zombie just hit you doing <strong>$LostHealth</strong> damage!</p>"; echo "<p>You now have $Health HP</p>"; } } ?> However, the $LostHealth is always a fixed number everytime it loops. Thanks for the help and time Edited August 24, 2015 by KillZoneZ Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 24, 2015 Solution Share Posted August 24, 2015 (edited) You need to set the random value inside the loop, not just once. while ($Health > 0) { $LostHealth = rand(1,10); $Health = $Health - $LostHealth; echo "<p>A Zombie just hit you doing <strong>$LostHealth</strong> damage!</p>"; echo "<p>You now have $Health HP</p>"; } Edited August 24, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
KillZoneZ Posted August 24, 2015 Author Share Posted August 24, 2015 Thank you very much sir Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.