royal1664 Posted August 2, 2013 Share Posted August 2, 2013 Hi, since learning PHP i've come across this and i'm a little confused. Was wondering if anyone could shed some light on it. I'm more than familiar with IF statements from C++. However i'm confused as to why you might use an ELSEIF... why does it not work like this (below)? IF(This is true) { do this } IF(this is true) { do this } IF(this is true) { do this } else { go home, you're drunk } I don't under stand why if you make the first IF statement every other IF statement has to be an ELSEIF. Many thanks, Royal1664 Quote Link to comment https://forums.phpfreaks.com/topic/280776-if-else-elseif/ Share on other sites More sharing options...
AbraCadaver Posted August 2, 2013 Share Posted August 2, 2013 From your example I don't understand what you don't understand: http://us2.php.net/elseif if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } Quote Link to comment https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443244 Share on other sites More sharing options...
Zane Posted August 2, 2013 Share Posted August 2, 2013 elseif extends the beginning IF statement so that you can have more conditions... Otherwise, you would end up checking any and all conditions one by one which means that more than one can be true.. With an elseif chain.... only one will be true. Quote Link to comment https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443248 Share on other sites More sharing options...
kicken Posted August 2, 2013 Share Posted August 2, 2013 You can use several if's like you showed, but it will not necessarily do as you intended. When you do several if's as shown, then they are all evaluated independantly. Depending on what the condition is that you check, it may emulate the same behaviour as using elseif. When you use elseif, then everything is treated as a single block so only one of the if statements (or else block) can be evaluated, all others are skipped. Take for example: $x = 52; if ($x < 100){ echo '< 100'; } else if ($x < 75){ echo '< 75'; } else if ($x < 50){ echo '< 50'; } else { echo 'None of the above'; } The output of that would be '< 100'. Since that if condition matches, all other elseif's and the else block are skipped. If however you wrote them as separate statements such as $x = 52; if ($x < 100){ echo '< 100'; } if ($x < 75){ echo '< 75'; } if ($x < 50){ echo '< 50'; } else { echo 'None of the above'; } You would get the output '< 100< 75None of the above' since each if is evaluated separately. The first if passes, the second if passes, the third doesn't pass so it's associated else block gets executed. Quote Link to comment https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443249 Share on other sites More sharing options...
Solution KevinM1 Posted August 2, 2013 Solution Share Posted August 2, 2013 AFAIK, if/else in PHP is the same as it is in C++ (which makes sense since PHP is written, I believe, in C). While C++ doesn't have a dedicated elseif language construct, it's very common to see else if (/* something */) in C++. If it helps, just turn elseif into: if (/* something */) { // blah } else { if (/* something else */) { // blah } } Quote Link to comment https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443258 Share on other sites More sharing options...
royal1664 Posted August 2, 2013 Author Share Posted August 2, 2013 Nice one, thanks mate! Quote Link to comment https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443262 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.