cloudll Posted August 18, 2011 Share Posted August 18, 2011 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/ Share on other sites More sharing options...
cloudll Posted August 18, 2011 Author Share Posted August 18, 2011 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/#findComment-1259111 Share on other sites More sharing options...
KevinM1 Posted August 18, 2011 Share Posted August 18, 2011 And the error is...? Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/#findComment-1259114 Share on other sites More sharing options...
cssfreakie Posted August 18, 2011 Share Posted August 18, 2011 Yes you can http://php.net/manual/en/control-structures.continue.php (use of continue) But you might want to have a look in to the switch statement http://www.php.net/manual/en/control-structures.switch.php Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/#findComment-1259116 Share on other sites More sharing options...
Psycho Posted August 18, 2011 Share Posted August 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/#findComment-1259119 Share on other sites More sharing options...
Psycho Posted August 18, 2011 Share Posted August 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/#findComment-1259121 Share on other sites More sharing options...
cloudll Posted August 18, 2011 Author Share Posted August 18, 2011 Thanks guys, Solved it thanks to all your inputs Quote Link to comment https://forums.phpfreaks.com/topic/245135-multiple-if-else/#findComment-1259169 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.