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.
elseif/else if
(PHP 4, PHP5)
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 urprü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 is 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. Die erste, deren Bedingung zutrifft, 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 (ähnlich 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 zur Definition der if/elseif-Bedingungen benutzt wird, darf else if nicht in zwei Wörtern geschrieben werden, oder PHP wird das Skript mit einem Parse Error abbrechen.
<?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;
?>
elseif/else if
27-Dec-2006 06:59
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.
26-Oct-2006 05:04
<?
if ($a==1){
/* nested if */
if ($b==1) {echo "2";}
}elseif ($a==3){
/* other code */
}
?>
"Bug" solved.
15-Aug-2006 04:02
This example generates a parse error:
<?
if ($a==1):
/* nested if */
if ($b==1) echo "2";
elseif ($a==3):
/* other code */
endif;
?>
The nested "if" binds to the outer "elseif" and the colon the generates an error: syntax error, unexpected ':'.
According to http://bugs.php.net/bug.php?id=838 this "bug" is not going to be fixed. Beware.
An "artful" sollution to this bug could look like this:
<?
if ($a==1):
/* nested if */
if ($b==1) echo "2";
/* dummy expression */
;
elseif ($a==3):
/* other code */
endif;
?>
11-Aug-2006 07:58
The comment critizing matheo's code while making some perhaps interesting statements about code efficiency missed the point completely.
echo 0; is different then echo false;
echo 0 will print a 0
echo false will print nothing.
So, the ternary operator technique was simply assuring there would be a displayable value.
If anything matheo showed prowess as a programmer and that he was knowledgable about nuances in php programming by knowing how to use the ternary operator to provide values true and false that are displayable.
Perhaps a clearer example of this would have been:
$is_a_bigger = ($a > $b) ? "true" : "false" ;
It is true that the result of a logic expression is a value like any other, it is a value though of a specific type which if using === can be checked for boolean logic which may or may not be what a programmer wants.
The general caveat if we are going to pontificate to programmers is to know exacly what you are getting from any operation and know the consequences of doing so. Which I think matheo knew quite well, and appropriately accounted for.
10-Apr-2005 05:44
Dont make your code as jumbled up as this:
if (!$username || !$password) {
echo("<form method='post' action='signin.php'>");
} elseif($action == "c" || $action == "d" || $action == "e" || $action == "f" || $action == "g" || $action == "h" || $action == "i") {
echo("<form method='post' action='" . findbattle() . ".php'><input type='hidden' name='strt' value='y'>");
} elseif($action == "b") {
die ("Sorry, dude. You're banned from this site!");
} else {
echo("<form method='post' action='game.php'>");
}
It works best when you only compare related variables.