Archived

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

if..elseif...is there a if or?

Recommended Posts

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 = 85

Now 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?

Share on other sites
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

Share on other sites
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?

Share on other sites
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]

Share on other sites
[code]
<?php

switch (TRUE){
case (\$i <= \$k):
echo "<br>image1";

case (\$i <= \$l):
echo "<br>image2";
break;

default:
echo "<br>image3";
break;
}

?>
[/code]

Share on other sites
craygo,

Now I see what you are saying,.. very good thanks.

And HeyRay2,

Is this a more efficient way to do that?

Share on other sites
Don't use elseif in the second statement, use if :)

Share on other sites
[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.

×

• Activity

• Chat
×
• Create New...