简体   繁体   English

if和elseif有什么区别?

[英]What's the difference between if and elseif?

This should be a simple question. 这应该是一个简单的问题。 I have a simple if/else statement: 我有一个简单的if / else语句:

    <?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. 我应该提一下,这是进入Wordpress的。 And until now, I've used the first part (no elseif, just a series of 'ifs'), and it works. 直到现在,我已经使用了第一部分(没有其他,只是一系列'ifs'),并且它有效。 I was just curious to know what the difference was. 我很想知道区别是什么。

Thanks! 谢谢! Amit 阿米特

Yes. 是。 If a condition in an if/else control is satisfied, the rest of the checks will be omitted. 如果满足if/else控制中的条件,则将省略其余检查。 else if is just a nested if inside an else ! else if仅仅是一个嵌套if内部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 if s, all of them will be tested. 但是在一系列if ,所有这些都将被测试。

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. 对于具有两个以上状态的条件,情况也是如此。

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. 第二种方法将检查每个条件,直到一个为真,然后忽略其余条件。

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. 此外,toppic将始终分配给is_page('newspaper')中的值或is_page('newspaper')的else语句中的值。 This happens because the last if statment is always evaluated. 发生这种情况是因为始终评估最后的if语句。 Even if one of the previous if statements evaluated to true, you'll end up in the else block. 即使之前的if语句之一被评估为true,您也会在else块中结束。 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'. 你总是以'newspaper.png'或'finalelse.png'结尾。

<?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: 但是与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. 所以你应该使用ELSEIF,因为否则$ toppic将总是导致'newspaper.png',它应该是正确的,或者'finalelse.png'可能是对还是错,因为它将覆盖先前的条件子句。

I hope you'll find this helpful. 我希望你会发现这很有帮助。

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 如果您正在切换某些内容,则必须使用else if ,而不仅仅if

Let's say we are toggling the variable $computerOn 假设我们正在切换变量$computerOn

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

In the case above your $computerOn will always be true . 在上面的情况下, $computerOn永远true If it's true , it is set to false . 如果它是true ,它被设置为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 . 在此之后,我们检查它是否为false ,它现在必须独立于初始条件,因此它现在设置为true

On the other hand the code below will toggle $computerOn: 另一方面,下面的代码将切换$ 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 . 现在我们检查$computerOn是否为false如果它最初不是true Hence we have a toggle. 因此我们有一个切换。

If things get more complicated, you might have to use multiple elseif s. 如果事情变得更复杂,你可能不得不使用多个elseif It's important to recognize when logic dictates that elseif is a must vs an option. 重要的是要认识到逻辑何时指示elseif是必须与选项。

The biggest difference between the two is that the very last else block will be called whenever is_page('newspaper') returns false. 两者之间最大的区别是,只要is_page('newspaper')返回false,就会调用最后一个else块。 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. 除此之外,如果使用单独的if语句,则每次都会评估每个if的条件。 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. 好吧,你应该能够看到它的发展方向;)如果你使用elseif ,一旦它变为真,它将停止尝试评估。 And, the else will only be called if nothing else is true. 并且,只有在没有其他条件成立的情况下才会调用else。

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. 如果a == 1且b == 1同时不可能。 Although when both if statements can be true, when b and c can be executed. 虽然两个if语句都可以为true,但是当b和c可以执行时。 This would not be possible if you use elsif there, because b==1 would only be checked if a!=1! 如果你在那里使用elsif,这是不可能的,因为只有在!= 1时才会检查b == 1!

明智地使用else可以节省大量时间,因为解析器不需要评估所有条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM