search for in the  
<Control Structureselseif>
Last updated: Thu, 19 May 2005

else

Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would display a is bigger than b if $a is bigger than $b, and a is NOT bigger than b otherwise:

<?php
if ($a > $b) {
   echo
"a is bigger than b";
} else {
   echo
"a is NOT bigger than b";
}
?>

The else statement is only executed if the if expression evaluated to FALSE, and if there were any elseif expressions - only if they evaluated to FALSE as well (see elseif).



User Contributed Notes
else
jak at mNuOnSkPeAeMs dot co dot uk
16-Mar-2005 04:42
Surely it should print "messages" for all values except 1. i.e. You have 0 messages, You have 1 message, You have 2 messages.
So wouldn't this be the easy answer...?

<?php
echo "You have $i message".($i!=1?"s":"")." in your mailbox.\n";
?>
15-Dec-2004 09:41
cgeng, the negative value is intentional:

echo "You have $i message".($i-1?"s":"")." in your mailbox.\n";

With one and only one message, this prints "You have 1 message in your mailbox." With any and all other values, it prints "You have # messages in your mailbox." This is grammatically correct for all values of $i.
cgeng at home dot se
09-Dec-2004 06:46
Stephens example is an example of tersity taken too far:

echo "You have $i message".($i-1?"s":"")." in your mailbox.\n";

gives a negative value of the expression if there are no messages, which, if defined, should be interpreted as false.
Caliban Darklock
08-Nov-2004 01:24
If you're coming from another language that does not have the "elseif" construct (e.g. C++), it's important to recognise that "else if" is a nested language construct and "elseif" is a linear language construct; they may be compared in performance to a recursive loop as opposed to an iterative loop.

<?php
$limit
=1000;
for(
$idx=0;$idx<$limit;$idx++) 
{
$list[]="if(false) echo \"$idx;\n\"; else"; }
$list[]=" echo \"$idx\n\";";
$space=implode(" ",$list);| // if ... else if ... else
$nospace=implode("",$list); // if ... elseif ... else
$start=array_sum(explode(" ",microtime()));
eval(
$space);
$end=array_sum(explode(" ",microtime()));
echo
$end-$start . " seconds\n";
$start=array_sum(explode(" ",microtime()));
eval(
$nospace);
$end=array_sum(explode(" ",microtime()));
echo
$end-$start . " seconds\n";
?>

This test should show that "elseif" executes in roughly two-thirds the time of "else if". (Increasing $limit will also eventually cause a parser stack overflow error, but the level where this happens is ridiculous in real world terms. Nobody normally nests if() blocks to more than a thousand levels unless they're trying to break things, which is a whole different problem.)

There is still a need for "else if", as you may have additional code to be executed unconditionally at some rung of the ladder; an "else if" construction allows this unconditional code to be elegantly inserted before or after the entire rest of the process. Consider the following elseif() ladder:

<?php
if($a) { conditional1(); }
elseif(
$b) { conditional2(); }
elseif(
$c) { conditional3(); }
elseif(
$d) { conditional4(); }
elseif(
$e) { conditional5(); }
elseif(
$f) { conditional6(); }
elseif(
$g) { conditional7(); }
elseif(
$h) { conditional8(); }
else {
conditional9(); }
?>

To insert unconditional preprocessing code for $e onward, one need only split the "elseif":

<?php
if($a) { conditional1(); }
elseif(
$b) { conditional2(); }
elseif(
$c) { conditional3(); }
elseif(
$d) { conditional4(); }
else {
....
unconditional();
....if(
$e) { conditional5(); }
....elseif(
$f) { conditional6(); }
....elseif(
$g) { conditional7(); }
....elseif(
$h) { conditional8(); }
....else {
conditional9(); }
}
?>

The alternative is to duplicate the unconditional code throughout the construct.
stephen at dotnetbytes dot com
29-Oct-2004 11:32
me, being a c/c++ programer, would be lazy and try to type as few characters as possible so that last statement:
echo "You have $i ". ($i==1 ? "message" : "messages"). " in your mailbox.\n";

can become:
echo "You have $i message".($i-1?"s":"")." in your mailbox.\n";

since if $i is 1 it ($i-1) would result in a 0 and evaluate as false. But if $i is more than 1 will be a positive integer and evaluate as true and add the 's'.
cap at capsi dot com
06-Oct-2000 12:58
Often you can avoid large if/else statements in your code by using the ternary operator. For example:

<?php
echo "You have $i ". ($i==1 ? "message" : "messages"). " in your mailbox.\n";
?>

<Control Structureselseif>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT