piznac Posted August 18, 2006 Share Posted August 18, 2006 Hello all,I have this statement:[code]if ($i <= $k) {echo "<br>image1'";}elseif ($i <= $l){echo "<br>image2";}else {echo "<br>image3";}[/code]Lets say for now:$i = 50$k = 86$l = 85Now with that stated both the first two conditions ring true. My problem is it will only display the first image. I assume this is due to the elseif statement beacause it did meet the first condition. What I want to happen is if both the first conditions are true then only display the 2nd image. Because even though both are true the 2nd one at that is the only one that matters at this point. BTW this for a job completion ratio, so if your behind by 14 points you would get a yellow image, if you are behind by 15 or more you will get a red image. But the way I have it written, if the first condition is true it stops there and displays the first image. This is not what I want. I hope someone can understand this :)So is there a way to do this? Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/ Share on other sites More sharing options...
craygo Posted August 18, 2006 Share Posted August 18, 2006 What you want to do is situate your condition in a certain order.[code]if ($i <= $k) {echo "<br>image1'";}elseif ($i <= $l && $i > $k){echo "<br>image2";}else {echo "<br>image3";}[/code]Ray Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76798 Share on other sites More sharing options...
piznac Posted August 18, 2006 Author Share Posted August 18, 2006 That dosent seem to work,.. I think with that "elseif" statement it stops the code right there. Because if condition1 is met there is no "else",.. right? Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76802 Share on other sites More sharing options...
craygo Posted August 18, 2006 Share Posted August 18, 2006 OK then situate it like so[code]if ($i <= $k && $i < $l) {echo "<br>image2";}elseif ($i <= $k){echo "<br>image1";}else {echo "<br>image3";}[code]Ray[/code][/code] Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76804 Share on other sites More sharing options...
HeyRay2 Posted August 18, 2006 Share Posted August 18, 2006 [code]<?php switch (TRUE){ case ($i <= $k): echo "<br>image1"; case ($i <= $l): echo "<br>image2"; break; default: echo "<br>image3"; break; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76806 Share on other sites More sharing options...
piznac Posted August 18, 2006 Author Share Posted August 18, 2006 craygo,Now I see what you are saying,.. very good thanks.And HeyRay2,Is this a more efficient way to do that? Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76809 Share on other sites More sharing options...
Ifa Posted August 18, 2006 Share Posted August 18, 2006 Don't use elseif in the second statement, use if :) Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76831 Share on other sites More sharing options...
HeyRay2 Posted August 18, 2006 Share Posted August 18, 2006 [code]<?php switch (TRUE){ case ($i <= $k): echo "<br>image1"; case ($i <= $l): echo "<br>image2"; break; default: echo "<br>image3"; break; }?>[/code]Depending on your goal, [b]switch()[/b] can be more efficient. It basically allows you to check a variable against multiple values or conditions.The [b]switch[/b] block I provided you will match all cases that evaluate to "TRUE", and perform an action under those matching cases.The [b]break[/b] lines tell the [b]switch()[/b] to stop executing, and no other cases will be checked. I left out the [b]break[/b] in the first case so that the first and second case would always be checked. If neither are matched, then the [b]default[/b] case executes.There is more than one way to accomplish what you are trying to do, I just prefer the [b]switch()[/b] method when I know I might need to match more than one conditional. Link to comment https://forums.phpfreaks.com/topic/17955-ifelseifis-there-a-if-or/#findComment-76840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.