Jump to content

if... else... & elseif


royal1664
Go to solution Solved by KevinM1,

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
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
Share on other sites

  • Solution

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