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
Yes. If a condition in an
if/else
control is satisfied, the rest of the checks will be omitted.else if
is just a nestedif
inside anelse
!But in a series of
if
s, all of them will be tested.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
than
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 biggest difference between the two is that the very last
else
block will be called wheneveris_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 eachif
is evaluated every time. Again, in this case, it's (probably) not a big deal. But, if the condition was, say...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.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.
It's not always just a question of efficiency. If you are toggling something, it is essential to use
else if
and not justif
Let's say we are toggling the variable
$computerOn
In the case above your
$computerOn
will always betrue
. If it'strue
, it is set tofalse
. After this, we check if it isfalse
, which it now must be independent of initial conditions, so it is now set totrue
.On the other hand the code below will toggle $computerOn:
Now we only check whether
$computerOn
isfalse
if it was not initiallytrue
. Hence we have a toggle.If things get more complicated, you might have to use multiple
elseif
s. It's important to recognize when logic dictates thatelseif
is a must vs an option.The answer is simple:
equals to
This is the same as
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!
Use elseif wisely can save you a bunch of time since the parser doesn't need to evaluate all the conditions.