Jump to content

if... else... & elseif


royal1664

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/280776-if-else-elseif/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443248
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443249
Share on other sites

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
    }
}
Link to comment
https://forums.phpfreaks.com/topic/280776-if-else-elseif/#findComment-1443258
Share on other sites

Archived

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

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