downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Alternatieve syntax voor control structures> <else
Last updated: Wed, 22 Jul 2009

view this page in

elseif

elseif, zoals de naam al suggereerd is een combinatie van de if en de else. Net zoals de else, breidt het de if uit met de mogelijkheid om een ander statement uit te voeren als de if expressie evalueerd naar FALSE. In tegenstelling tot else zal elseif het statement alleen uitvoeren als de elseif expressie evalueerd tot TRUE. Als voorbeeld zal de code hieronder a is groter dan b, a is gelijk aan b of a is kleiner dan b tonen:

if ($a > $b) {
    print "a is groter dan b";
} elseif ($a == $b) {
    print "a is gelijk aan b";
} else {
    print "a is kleiner dan b";
}

Je kunt meerdere elseifs binnen hetzelfde if statement gebruiken. De eerste elseif expressie (indien aanwezig) die evalueerd tot TRUE zal worden uitgevoerd. In PHP kun je ook gebruiken 'else if' (in twee woorden) en de werking zou precies identiek zijn aan één 'elseif' (in één woord). De syntactische betekenis is heel iets anders (net zoals in C), maar beide zullen precies hetzelfde resultaat opleveren.

Het elseif statement wordt alleen maar uitgevoerd indien alle voorgaande if expressies én alle voorafgaande elseif expressies evalueren tot FALSE, en de huidige elseif expressie evalueerd tot TRUE.



add a note add a note User Contributed Notes
elseif
31-Jan-2007 11:54
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.
Vladimir Kornea
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.

Alternatieve syntax voor control structures> <else
Last updated: Wed, 22 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites