elseif/else if

(PHP 4, PHP 5)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

<?php
if ($a $b) {
    echo 
"a is bigger than b";
} elseif (
$a == $b) {
    echo 
"a is equal to b";
} else {
    echo 
"a is smaller than b";
}
?>

There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.

Notă: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

<?php

/* Incorrect Method: */
if($a $b):
    echo 
$a." is greater than ".$b;
else if(
$a == $b): // Will not compile.
    
echo "The above line causes a parse error.";
endif;


/* Correct Method: */
if($a $b):
    echo 
$a." is greater than ".$b;
elseif(
$a == $b): // Note the combination of the words.
    
echo $a." equals ".$b;
else:
    echo 
$a." is neither greater than or equal to ".$b;
endif;

?>

add a note add a note

User Contributed Notes 6 notes

up
24
Vladimir Kornea
7 years ago
The parser doesn't handle mixing alternative if syntaxes as reasonably as possible.

The following is illegal (as it should be):

<?
if($a):
    echo $a;
else {
    echo $c;
}
?>

This is also illegal (as it should be):

<?
if($a) {
    echo $a;
}
else:
    echo $c;
endif;
?>

But since the two alternative if syntaxes are not interchangeable, it's reasonable to expect that the parser wouldn't try matching else statements using one style to if statement using the alternative style. In other words, one would expect that this would work:

<?
if($a):
    echo $a;
    if($b) {
      echo $b;
    }
else:
    echo $c;
endif;
?>

Instead of concluding that the else statement was intended to match the if($b) statement (and erroring out), the parser could match the else statement to the if($a) statement, which shares its syntax.

While it's understandable that the PHP developers don't consider this a bug, or don't consider it a bug worth their time, jsimlo was right to point out that mixing alternative if syntaxes might lead to unexpected results.
up
-20
Anonymous
7 years ago
There is no good way to interpret the dangling else.  One must pick a way and apply rules based on that. 

Since there is no endif before an else, there is no easy way for PHP to know what you mean.
up
-22
Anonymous
8 months ago
In addition to what Rudi  wrote,
I ran his test script  on Debian 6 with php 5.3.26 where his findings are true, elseif is faster than else if.
Then I ran his script on Windows 7 with php 5.4.7, where results are the opposite, else if is faster than elseif.
up
-17
peter dot mlich at volny dot cz
5 months ago
To  Rudi / 3 years ago

Try switch in switch($name) case 'word': break; . --- slow
Try if/else and  if/elseif in $name='word'. --- in my fast test, place 3
Try isset in isset($array[$name]). ---  place 1
Try in_array in in_array($name,$array). --- slow
Try array_key_exists in array_key_exists($name,$array).

Try return (end function) in if/elseif with  -- place 2
if('word'==$name) {
  $parsed[$name]=$text;
  return;
  } 
elseif('word'==$name) {
  $parsed[$name]=$text;
  return;


PHP 5.35, xml_parse function, i parsed 9.2MB xml file to sql, script in place 1 do it at 11.54s (do more than only condition)
up
-40
Anonymous
8 months ago
The word 'elseif' is a not a combination of "if and else," but a combination of "else and if."
up
-65
Rudi
4 years ago
Note that } elseif() { is somewhat faster than } else if() {

===================================
Test (100,000,000 runs):
<?php
$start
= microtime(true);
for(
$i = 0; $i < 100000000; $i++) {
    if(
2 === 0) {} else if(2 === 1) {} else {}
}
$end = microtime(true);
echo
"1: ".($end - $start)."\n";
unset(
$start, $end);

$start = microtime(true);
for(
$i = 0; $i < 100000000; $i++) {
    if(
2 === 0) {} elseif(2 === 1) {} else {}
}
$end = microtime(true);
echo
"2: ".($end - $start);
?>

===================================
Result (depending on hardware configuration):
1: 20.026723146439
2: 20.20437502861
To Top