Jump to content

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


piznac

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?
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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