I’m trying to display an image based on a change in rank. If the values of the array $rank_change are echoed before my if statements, the correct values are printed, such as:
0, 0, 0, 0, 0, 0, 0, -7, 1, 1, 1, 1, 1, 1, 1
However, if printed after the following if statements, side.gif is always shown:
if($rank_change[$i] > 0) { $rank_change[$i] = "<img src=\"up.gif\" width=\"16\" height=\"16\"/>"; }
if($rank_change[$i] < 0) { $rank_change[$i] = "<img src=\"down.gif\" width=\"16\" height=\"16\"/>"; }
if($rank_change[$i] == 0) { $rank_change[$i] = "<img src=\"side.gif\" width=\"16\" height=\"16\"/>";}
In the cases where the value of $rank_change are 7 and 1, why does the last statement still evaluate to true?
I realize it would be more efficient to use switch($rank_change[$i]) but, I still don’t understand why the final if statement is evaluating true on all values.
Any help would be greatly appreciated!
use an else-if instead of a simple if for the last two conditions.
you are actually overwriting the value in
$rank_change[$i]in the expression following each test.$rank_change[$i] = "<img src=\"up.gif\" width=\"16\" height=\"16\"/>"will change the value of$rank_change[$i], which will make one of the following test return true as well, resulting in another change of the value of$rank_change[$i]…use an
else ifconstruct, and only one test will succeed.