views:

187

answers:

9

This should be a simple question. I have a simple if/else statement:

    <?php
    // TOP PICTURE DEFINITIONS
    if ( is_page('english') ) {
        $toppic = 'page1.png';
    }
    if ( is_page('aboutus') ) {
        $toppic = 'page1.png';
    }
    if ( is_page('newspaper') ) {
        $toppic = 'page1.png';
    }
    else {
        $toppic = 'page1.png';
    }
?>

Is there a difference from ^^^ to this:

<?php
    // TOP PICTURE DEFINITIONS
    if ( is_page('english') ) {
        $toppic = 'page1.png';
    }
    elseif ( is_page('aboutus') ) {
        $toppic = 'page1.png';
    }
    elseif ( is_page('newspaper') ) {
        $toppic = 'page1.png';
    }
    else {
        $toppic = 'page1.png';
    }
?>

I should mention that this is going into Wordpress. And until now, I've used the first part (no elseif, just a series of 'ifs'), and it works. I was just curious to know what the difference was.

Thanks! Amit

+11  A: 

Yes. If a condition in an if/else control is satisfied, the rest of the checks will be omitted. else if is just a nested if inside an else!

if ( is_page('english') ) { // if true, other statements are skipped
    $toppic = 'page1.png';
}
elseif ( is_page('aboutus') ) {
    $toppic = 'page1.png';
}
elseif ( is_page('newspaper') ) {
    $toppic = 'page1.png';
}
else {
    $toppic = 'page1.png';
}

But in a series of ifs, all of them will be tested.

if ( is_page('english') ) {
    $toppic = 'page1.png';
}
if ( is_page('aboutus') ) { // will be tested no matter what the outcome
                            // of the previous if statement was
    $toppic = 'page1.png';
}
if ( is_page('newspaper') ) { // the same here
    $toppic = 'page1.png';
}
else {
    $toppic = 'page1.png';
}

So, if you're checking a property such as gender, it's either male or female, why do you want to bother checking other conditions if one is satisfied. It's waste of resources. Therefore, the following code is much better

if(gender == 'male')
{
    // do something
}
else // if it's not male, it's female for sure
{
    // do some other thing
}

than

if(gender == 'male')
{
    // do something
}
if(gender == 'female')
{
    // do some other thing
}

Because the former checks the condition once whilst the latter does it twice. This is just an example, the same thing goes for conditions with more than two states.

Hamid Nazari
Got it. Thanks for making it clearer!
Amit
I've seen worse examples in PHP guidebooks. Good job!
Andrew Heath
+2  A: 

The first method will check against every condition, whether they are true or false.

The second method will check against every condition until one is true, and then ignores the rest.

Shane Reustle
+2  A: 

In your first block, every comparison in your block is executed. Also, toppic will always be assigned the value in is_page('newspaper') or the value in is_page('newspaper')'s else statement. This happens because the last if statment is always evaluated. Even if one of the previous if statements evaluated to true, you'll end up in the else block. To test this, try this code...

<?php
    // TOP PICTURE DEFINITIONS
    if ( is_page('english') ) {
        $toppic = 'english.png';
    }
    if ( is_page('aboutus') ) {
        $toppic = 'aboutus.png';
    }
    if ( is_page('newspaper') ) {
        $toppic = 'newspaper.png';
    }
    else {
        $toppic = 'finalelse.png';
    }
?>

You'll always end with either 'newspaper.png' or 'finalelse.png'.

Paul Whitehurst
+1  A: 

The biggest difference between the two is that the very last else block will be called whenever is_page('newspaper') returns false. In this case, it means just about every time the script runs. In this case, it's not a big deal, since you're only setting a variable, and it's the same value as everything else. But, if it were different, you would have a very frustrating bug to track down!

Besides that, if you use separate if statements, the condition for each if is evaluated every time. Again, in this case, it's (probably) not a big deal. But, if the condition was, say...

if(delete_file('foo.png')) {
    ....
}

if(delete_file('bar.png')) {
    ....
}

if(delete_file('baz.png')) {
    ....
}
else {
    ....
}

Well, you should be able to see where this is going ;) If you use elseif, it will stop trying to evaluate once it gets a true. And, the else will only be called if nothing else is true.

Mike Caron
+2  A: 
<?php
    if ( 3 > 1 ) {
        echo "This will be printed.";
    }
    if ( 3 > 2 ) {
        echo "This will be printed too.";
    }
    if ( 3 > 3 ) {
        echo "This will NOT be printed.";
    }
    else {
        echo "This WILL be printed.";
    }
?>

but with elseif:

<?php
    if ( 3 > 1 ) {
        echo "This will be printed.";
    }
    elseif ( 3 > 2 ) {   /* This condition will not be evaluated */
        echo "This will NOT be printed";
              // because it's on the ELSE part of the previous IF
    }   
    elseif ( 3 > 3 ) {   /* This condition will not be evaluated either */
        echo "This will NOT be printed.";
    }
    else {    /* This ELSE condition is still part of the first IF clause */
        echo "This will NOT be printed.";
    }
?>

So you should use ELSEIF, because otherwise $toppic will always result on either 'newspaper.png', wich should be right, or 'finalelse.png' wich could be right or wrong, because it will overwrite the previous conditional clauses.

I hope you'll find this helpful.

Sebastián Grignoli
+1  A: 

The answer is simple:

if(a==1){
  b
}
elsif(b==1){
  c
}

equals to

if(a==1){
  b
}
else{
  if(b==1){
    c
  }
}

This is the same as

if(a==1){
  b
}
if(b==1){
  c
}

if it is not possible that a==1 and b==1 at the same time. Although when both if statements can be true, when b and c can be executed. This would not be possible if you use elsif there, because b==1 would only be checked if a!=1!

Patrick Cornelissen
A: 

can anyone give more example scenarios for this..On when to use multiple if and elseif

Sreeja
This is not an answer. This is a comment. You could try deleting this, and adding it as a comment to the question.
Peter Ajtai
A: 

Use elseif wisely can save you a bunch of time since the parser doesn't need to evaluate all the conditions.

Truong Ha
+1  A: 

It's not always just a question of efficiency. If you are toggling something, it is essential to use else if and not just if

Let's say we are toggling the variable $computerOn

if ($computerOn == true) {
    $computerOn = false;
}
if ($computerOn == false) {
    $computerOn = true;
}

In the case above your $computerOn will always be true. If it's true, it is set to false. After this, we check if it is false, which it now must be independent of initial conditions, so it is now set to true.

On the other hand the code below will toggle $computerOn:

if ($computerOn == true) {
    $computerOn = false;
} elseif ($computerOn == false) {
    $computerOn = true;
}

Now we only check whether $computerOn is false if it was not initially true. Hence we have a toggle.

If things get more complicated, you might have to use multiple elseifs. It's important to recognize when logic dictates that elseif is a must vs an option.

Peter Ajtai
Love the example. Thank you
Amit