What's the difference between if and elseif?

2020-04-08 08:02发布

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

8条回答
2楼-- · 2020-04-08 08:13

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 parity of a number, it's either odd or even, why do you want to bother checking other conditions if one is satisfied. It's a waste of resources. Therefore, the following code is much better

if(number_is_odd) {
}
else { // if it's not odd, it's even for sure
}

than

if(number_is_odd) {
}

if(!number_is_odd) {
}

Because the former checks the condition once whilst the latter does it twice. The same thing goes for conditions with more than two states.

查看更多
孤傲高冷的网名
3楼-- · 2020-04-08 08:14

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.

查看更多
贪生不怕死
4楼-- · 2020-04-08 08:21

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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-04-08 08:21

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.

查看更多
家丑人穷心不美
6楼-- · 2020-04-08 08:22

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!

查看更多
乱世女痞
7楼-- · 2020-04-08 08:22

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

查看更多
登录 后发表回答