spfoonnewb Posted February 28, 2007 Share Posted February 28, 2007 Hi, I am working on a science PHP script.. but I cannot seem to get it to add and subtract correctly, it gives up when it reached 5, and no longer increments.. it works from the beginning.. and stops... I want it to run 25 times, but I want the variable to keep changed based on the previous run.. thus the for loop. <?php $TSH_total = "17"; $T_total = "11"; $TSH_bloodstream = "5"; $T_bloodstream = "0"; for ($i = 1; $i <= 25; ++$i) { if ($TSH_bloodstream == "5") { //Do Nothing } elseif ($TSH_bloodstream < "5") { $TSH_bloodstream--; $TSH_total++; } elseif ($TSH_bloodstream > "5") { $TSH_total--; $TSH_bloodstream++; } if ($T_bloodstream == "5") { //Do Nothing } elseif ($T_bloodstream < "5") { $T_total--; $T_bloodstream++; } elseif ($T_bloodstream > "5") { $T_bloodstream--; $T_total++; } echo "<table cellpadding=\"8\" cellspacing=\"8\" border=\"1\">"; echo "<tr><td>TSH</td><td>T</td></tr>"; echo "<tr><td>$TSH_bloodstream</td>"; echo "<td>$T_bloodstream</td></tr>"; echo "</table>"; } echo "<br />"; echo $TSH_total; //echos 17 echo "<br />"; echo $T_total; //echos 6 ?> It currently outputs: TSH T 5 1 TSH T 5 2 TSH T 5 3 TSH T 5 4 TSH T 5 5 TSH T 5 5 TSH T 5 5 and so on... 5:5 It should look more like: TSH T 6 1 TSH T 7 2 TSH T 8 3 TSH T 9 4 Things would start changing at 5.. Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted February 28, 2007 Share Posted February 28, 2007 Looking at your code... Once you reach a state where $TSH_bloodstream == 5 AND $T_bloodstream == 5 you will remain there forever. Have a look at the following code fragments: <?php if ($TSH_bloodstream == "5") { //Do Nothing } // ... if ($T_bloodstream == "5") { //Do Nothing } ?> As a side note you are treating all integers as Strings by putting them in quotes. This works because PHP is a loosely typed language, however this generally isn't a good idea you should remove all of the quotes from your integer values. Best, Patrick Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted February 28, 2007 Author Share Posted February 28, 2007 I am not really sure how I would fix that Quote Link to comment Share on other sites More sharing options...
btherl Posted February 28, 2007 Share Posted February 28, 2007 To fix it, you need to tell us what algorithm you are implementing. Right now we are guessing your algorithm based on your code. And obviously your code does not implement the algorithm you want it to Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted February 28, 2007 Author Share Posted February 28, 2007 *Ignore* 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.