elseif/else if

(PHP 4, PHP 5, PHP 7, PHP 8)

elseif, wie der Name schon sagt, ist eine Kombination aus if und else. Wie else erweitert es eine if-Kontrollstruktur, um alternative Befehle auszuführen, wenn die ursprüngliche if-Bedingung nicht zutrifft. Im Gegensatz zu else werden die Alternativ-Befehle aber nur ausgeführt, wenn die elseif-Bedingung zutrifft. Der folgende Beispielcode gibt a ist größer als b, a ist gleich groß wie b oder a ist kleiner als b aus:

<?php
if ($a > $b) {
echo
"a ist größer als b";
} elseif (
$a == $b) {
echo
"a ist gleich groß wie b";
} else {
echo
"a ist kleiner als b";
}
?>

Innerhalb einer if-Kontrollstruktur können mehrere elseif-Strukturen benutzt werden. Der erste elseif-Ausdruck (falls vorhanden), der zu true ausgewertet wird, wird ausgeführt. In PHP kann ebenfalls als Schlüsselwort else if (in zwei Wörtern) benutzt werden, was sich komplett identisch wie elseif (in einem Wort) verhält. Die syntaktische Bedeutung ist geringfügig anders (dasselbe Verhalten wie in C), aber das Ergebnis ist, dass beide sich exakt genauso verhalten.

Der elseif-Teil wird nur ausgeführt, wenn die vorhergehende if-Bedingung und alle vorhergehenden elseif-Bedingungen nicht zutrafen (false) und die aktuelle elseif-Bedingung zutrifft (true).

Hinweis: Achtung: elseif und else if verhalten sich nur gleich, wenn geschwungene Klammern verwendet werden, wie im obigen Beispiel. Wenn ein Doppelpunkt verwendet wird, um if/elseif-Bedingungen zu definieren, muss elseif in einem einzigen Wort verwendet werden. Wenn else if in zwei Wörter aufgeteilt wird, gibt PHP einen Parse-Fehler aus.

<?php

/* Falsch: */
if ($a > $b):
echo
$a." ist größer als ".$b;
else if (
$a == $b): // Funktioniert nicht.
echo "Die vorige Zeile wird einen Parse Error verursachen.";
endif;


/* Richtig: */
if ($a > $b):
echo
$a." ist größer als ".$b;
elseif (
$a == $b): // elseif in einem Wort!
echo $a." ist gleich groß wie ".$b;
else:
echo
$a." ist weder größer als noch gleich wie ".$b;
endif;

?>

add a note add a note

User Contributed Notes 3 notes

up
2
Vladimir Kornea
17 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
-1
mparsa1372 at gmail dot com
3 years ago
The if...elseif...else statement executes different codes for more than two conditions.

Syntax:

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example:
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

<?php
$t
= date("H");

if (
$t < "10") {
  echo
"Have a good morning!";
} elseif (
$t < "20") {
  echo
"Have a good day!";
} else {
  echo
"Have a good night!";
}
?>
up
-12
qualitycoder
9 years ago
The reason 'else if' (with a space) works with traditional syntax and not colon syntax is because of a technicality.

<?php
 
if($var == 'Whatever') {

  } else if(
$var == 'Something Else') {

  }
?>

In this instance, the 'else if' is a shorthand/inline else statement (no curly braces) with the if statement as a body. It is the same things as:

<?php
 
if($var == 'Whatever') {

  } else {
      if(
$var == 'Something Else') {

      }
  }
?>

If you were to write this with colon syntax, it would be:

<?php
 
if($var == 'Whatever'):

  else:
      if(
$var == 'Something Else'):

      endif;
  endif;
?>
To Top