Jump to content

multiple if else


cloudll

Recommended Posts

I am trying to make a if else code with multiple if else statements,

 

I tried the following code, but it stops after the first if else condition is met. Is there a way to make it so it keeps going?

 

<?php

if ($exp < 1000) {

  echo '1';

} elseif ($exp > 3000) {

  echo '2';

} elseif ($exp > 8000) {

echo '3';

} elseif ($exp > 16000) {

echo '4';

}
?>

Link to comment
Share on other sites

I changed it to this now but i am getting an error

 

<?php

if ($exp < 1000) {

  echo '1';

} elseif ($exp > 3000) && ($exp <= 8000)) {

  echo '2';

} elseif ($exp > 8000) && ($exp <= 16000)) {

echo '3';

} elseif ($exp > 16000) && ($exp <= 25000)) {

echo '4';

}
?>


Link to comment
Share on other sites

Your change will not produce different results than what you have. You state you want it to continue after the first condition is met. So, just have a list of IF statemetns (not ifelse) and each one will be interpreted individually. If you want more help give an example of some input values and what you need for the output.

Link to comment
Share on other sites

But you might want to have a look in to the switch statement

http://www.php.net/manual/en/control-structures.switch.php

 

I would agree that the switch statement might be a solution, but it would require a slightly unintuitive implementation since the switch value is compared to the case values. You can't, AKAIK, do a mathematical comparison between the value in the switch to the value in the case. But, there is a workaround that I have seen used before that will work:

switch(true)
{
    case ($val < 1000):
       echo "1";
       break;

    case ($val > 3000):
       echo "2";
       break;

    case ($val > 8000):
       echo "3";
       break;

    case ($val > 16000):
       echo "4";
       break;
}

 

However, a switch statement is kind of all or nothing. Once it finds a match it will perform all the code in the remaining case statements unless there is a break in which case it will stop. So, you can't have it find a match with one and then check if there is a match with the remaining. Per the original post he wants it to continue checking after the first match.

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.