shergold Posted July 5, 2009 Share Posted July 5, 2009 Hey, i was wondering if someone would be kind enough to help me fix this while statement. Sorry if its a stupid mistake but im just learning php and this has been bugging me for the past hour. <?php //player $lvl = 10; $maxhit = $lvl *15; $minhit = $lvl *15/4; $yourhp = $lvl *100; //opponent $oplvl = 8; $opmaxhit = $oplvl *15; $opminhit = $oplvl *15/4; $hp = $oplvl *100; while ($hp>0 && $yourhp>0) { $yourhit = rand($minhit,$maxhit); $hp - $yourhit; echo "You hit {$yourhit}, leaving your opponent with {$hp}hp!</br>"; } ?> When i execute the php file i get an infinite loop, the echo statement displays a random number correctly but the hit isnt subtracted from the hp. for example: You hit 57, leaving your opponent with 800hp!. only the random number changes but the hp stays on 800. Thanks, Shergold. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/ Share on other sites More sharing options...
.josh Posted July 5, 2009 Share Posted July 5, 2009 $hp - $yourhit; Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869166 Share on other sites More sharing options...
shergold Posted July 5, 2009 Author Share Posted July 5, 2009 That is what i have put? Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869167 Share on other sites More sharing options...
haku Posted July 5, 2009 Share Posted July 5, 2009 That's where your error is. You didn't assign that to anything. You can use: $hp = $hp - $yourhit; but the code below is shorthand for that, and does the same thing: $hp -= $yourhit; Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869169 Share on other sites More sharing options...
shergold Posted July 5, 2009 Author Share Posted July 5, 2009 ah -.- thankyou i didnt realise i had to do that. also when the hp has gone down to a certain point say "17", and the $hit value is for example "100" it will stop. could you give me an example as to how i can get around that and make it take off the remaining hp amount rather than stopping when the hit amount is greater than the total hp? Thanks again, Shergold. Edit: i just added this to the while statement: if ($yourhit>$hp) $hp= $hp - $hp; but the hp goes into minus :S Edit again: actually the while statement goes into minus without the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869172 Share on other sites More sharing options...
haku Posted July 5, 2009 Share Posted July 5, 2009 Not really sure what you mean - are you ending up with a negative hit point value or a positive hit point value? Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869176 Share on other sites More sharing options...
shergold Posted July 5, 2009 Author Share Posted July 5, 2009 Here is an example: You hit 106, leaving your opponent with 694hp! You hit 118, leaving your opponent with 576hp! You hit 39, leaving your opponent with 537hp! You hit 150, leaving your opponent with 387hp! You hit 147, leaving your opponent with 240hp! You hit 136, leaving your opponent with 104hp! You hit 133, leaving your opponent with -29hp! that is without the if statement i tried, i want it so that if the hit is greater than the hp, just the remaining hp amount is taken off so the end value is 0. Thanks, Shergold. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869178 Share on other sites More sharing options...
.josh Posted July 5, 2009 Share Posted July 5, 2009 $hp = ($hp < 0)? 0 : $hp; echo "..."; Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869179 Share on other sites More sharing options...
shergold Posted July 5, 2009 Author Share Posted July 5, 2009 ah sorry as i said before im a noob , i dont understand any of this: $hp = ($hp < 0)? 0 : $hp; sorry but please could you explain it to me and how i can implement that into my code :S thanks, shergold. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869187 Share on other sites More sharing options...
nbarone Posted July 5, 2009 Share Posted July 5, 2009 ah sorry as i said before im a noob , i dont understand any of this: $hp = ($hp < 0)? 0 : $hp; sorry but please could you explain it to me and how i can implement that into my code :S thanks, shergold. it means if $hp is less than 0, make it 0, if not, make it $hp (more than 0) Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869189 Share on other sites More sharing options...
haku Posted July 5, 2009 Share Posted July 5, 2009 To add to that: Replace the code I gave you before with that. What it means is: If the thing in brackets is true, then assign the value of the thing after the question mark to the variable, and if it's false, then assign the thing after the colon to the variable. For example: $a = (1==1) ? $b : $c; You are assigning either $b or $c to $a. Since 1 does equal 1, after this code, $a will be equal to $b. $a = (1 == 2) ? $b : $c; In this case, 1 doesn't equal 2, so after this code, $a will be equal to $c. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869190 Share on other sites More sharing options...
shergold Posted July 5, 2009 Author Share Posted July 5, 2009 i just replaced the $hp -= $yourhit; with the new code but its taken me back to an infinite loop again with the hp staying at 800 :S, im really sorry if ive done it wrong -.- sorry for taking up your time. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869193 Share on other sites More sharing options...
shergold Posted July 5, 2009 Author Share Posted July 5, 2009 doesnt matter, i have got it working. Thanks allot for everyones help, shergold. Quote Link to comment https://forums.phpfreaks.com/topic/164839-solved-while-statement-help-noob/#findComment-869194 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.