seany123 Posted April 30, 2009 Share Posted April 30, 2009 <?php $random1 = rand(75,100); $random2 = rand(20,30); $random3 = ($random2 * 0.001); $statgain = ($player->energy * $player->awake / $random1); $statgain2 = ($_POST['energy_str'] * $player->awake / $random1); $statgain3 = ($_POST['energy_def'] * $player->awake / $random1); $statgain4 = ($_POST['energy_spd'] * $player->awake / $random1); // Awake loss. $awakeloss = ($statgain * $random3); $awakeloss2 = ($statgain2 * $random3); $awakeloss3 = ($statgain3 * $random3); $awakeloss4 = ($statgain4 * $random3); if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy) { echo"you dont have enough energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } else if ($_POST['energy_str']) { if($player->energy < 1 || $_POST['energy_str'] > $player->energy) { echo "<br>You dont have any energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_str'], ($player->awake - $awakeloss2) < 0 ? 0 : $player->awake - $awakeloss2, $player->strength + $statgain2, $player->total + $statgain2, $player->id )); echo "<br>You trained\n" . $_POST['energy_str'] . "\ntimes and You gained " . number_format($statgain2, 2, ".", ",") . " Strength<br>" ; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy) { echo"you dont have enough energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } else if ($_POST['energy_def']) { if($player->energy < 1 || $_POST['energy_def'] > $player->energy) { echo "<br>You dont have any energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_def'], ($player->awake - $awakeloss3) < 0 ? 0 : $player->awake - $awakeloss3, $player->defense + $statgain3, $player->total + $statgain3, $player->id )); echo "<br>You trained\n" . $_POST['energy_def'] . "\ntimes and You gained " . number_format($statgain3, 2, ".", ",") . " Defense<br>" ; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy) { echo"you dont have enough energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } else if ($_POST['energy_spd']) { if($player->energy < 1 || $_POST['energy_spd'] > $player->energy) { echo "<br>You dont have any energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_spd'], ($player->awake - $awakeloss4) < 0 ? 0 : $player->awake - $awakeloss4, $player->speed + $statgain4, $player->total + $statgain4, $player->id )); echo "<br>You trained\n" . $_POST['energy_spd'] . "\ntimes and You gained " . number_format($statgain4, 2, ".", ",") . " Speed<br>" ; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } ?> <form method='post'><br> <table width='80%' align='center'> <tr> <td rowspan='3' width='10'> </td> <td rowspan='3' width='100'> </td> <td align='center'><b>Strength</b></td> <td align='center'><b>Defense</b></td> <td align='center'><b>Speed</b></td> <td rowspan='3' width='10'> </td> </tr> <tr> <td align='center'><input type='text' name='energy_str' value='0' size='5' maxlength='5'></td> <td align='center'><input type='text' name='energy_def' value='0' size='5' maxlength='5'></td> <td align='center'><input type='text' name='energy_spd' value='0' size='5' maxlength='5'></td> </tr> <tr> <td colspan='7' align='center'><input type='submit' name='train' value='Train'></td> </tr> </table> </form> basically im having a problem, when i put any other values in the defense or speed... it will echo the message.. but it wont run the query. i think i might not needing half the exit; s in this code, the idea is that the user can put a value in each box then hit submit and it will run all 3 queries according to the value. Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/ Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 exit quits the script so nothing below it will run. Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822861 Share on other sites More sharing options...
seany123 Posted April 30, 2009 Author Share Posted April 30, 2009 but if i have a exit; in a if which isnt true... then it wont stop the script right? Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822882 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 Wrong. exit quits the script so nothing below it will run. But don't take my word for it. Here: http://us2.php.net/exit Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822884 Share on other sites More sharing options...
seany123 Posted April 30, 2009 Author Share Posted April 30, 2009 right so, would break; work? or what could i do to fix this problem then? Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822890 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 No, I think break only works in loops. But what are you trying to do? Just remove all those exits and it should be fine. Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822891 Share on other sites More sharing options...
seany123 Posted April 30, 2009 Author Share Posted April 30, 2009 well the immeadiate issue is that for some reason the queries for full_defense and full_speed arnt running, yet the echos are. i dont know why or how this can happen. i must be something to do with this part of the script, <?php if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy) { echo"you dont have enough energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } if ($_POST['energy_str']) { $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_str'], ($player->awake - $awakeloss2) < 0 ? 0 : $player->awake - $awakeloss2, $player->strength + $statgain2, $player->total + $statgain2, $player->id )); echo "<br>You trained\n" . $_POST['energy_str'] . "\ntimes and You gained " . number_format($statgain2, 2, ".", ",") . " Strength<br>" ; echo"<br><br><a href=\"gym.php\">Back</a>"; } if ($_POST['energy_def']) { $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_def'], ($player->awake - $awakeloss3) < 0 ? 0 : $player->awake - $awakeloss3, $player->defense + $statgain3, $player->total + $statgain3, $player->id )); echo "<br>You trained\n" . $_POST['energy_def'] . "\ntimes and You gained " . number_format($statgain3, 2, ".", ",") . " Defense<br>" ; echo"<br><br><a href=\"gym.php\">Back</a>"; } if ($_POST['energy_spd']) { $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_spd'], ($player->awake - $awakeloss4) < 0 ? 0 : $player->awake - $awakeloss4, $player->speed + $statgain4, $player->total + $statgain4, $player->id )); echo "<br>You trained\n" . $_POST['energy_spd'] . "\ntimes and You gained " . number_format($statgain4, 2, ".", ",") . " Speed<br>" ; echo"<br><br><a href=\"gym.php\">Back</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822906 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 One more exit to remove. Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822909 Share on other sites More sharing options...
seany123 Posted April 30, 2009 Author Share Posted April 30, 2009 opps, i just realised i have `strength`=? on all 3 queries >.< on a side note, everything is working fine now, and that exit seems to be working only when the if statement is true. Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-822947 Share on other sites More sharing options...
mrMarcus Posted April 30, 2009 Share Posted April 30, 2009 exit() is fundamentally the same as die(); it's good practice to use exit(); after header() redirects so as to avoid the further execution of code. using exit or die will stop the execution of the script you are running .. so, if the script reaches either command, no further IF/ELSE, etc. statements, or anything for that matter, will execute. you should definitely avoid using exit() in the manner you were/are. there are much cleaner, practical ways to finish scripts than to just hit it with an exit()/die(). Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-823047 Share on other sites More sharing options...
seany123 Posted May 1, 2009 Author Share Posted May 1, 2009 exit() is fundamentally the same as die(); it's good practice to use exit(); after header() redirects so as to avoid the further execution of code. using exit or die will stop the execution of the script you are running .. so, if the script reaches either command, no further IF/ELSE, etc. statements, or anything for that matter, will execute. you should definitely avoid using exit() in the manner you were/are. there are much cleaner, practical ways to finish scripts than to just hit it with an exit()/die(). okay thanks for the information... from what ive understood from this thread and reading others... exit; will kill the script even if its called in a if statement which is false... if this is true then my code above shouldnt work with any in... however it does and the exit; doesnt get used. Quote Link to comment https://forums.phpfreaks.com/topic/156298-solved-is-there-improper-use-of-exits-in-this-code/#findComment-823156 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.