Jump to content

[SOLVED] Addition/Subtraction Issues


spfoonnewb

Recommended Posts

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..

Link to comment
https://forums.phpfreaks.com/topic/40448-solved-additionsubtraction-issues/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.