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

search for in the

Alternativní syntaxe řídicích struktur> <else
Last updated: Sat, 24 Mar 2007

view this page in

elseif

Jak název napovídá, elseif, je kombinací if a else. Stejně jako else, rozšiřuje příkaz if k provádění odlišných příkazů v případě, že jevýraz původního příkazu if ohodnocen jako FALSE. Tedy, narozdíl od else, se provádí pouze tehdy, je-li výraz v podmínce elseif ohodnocen jako TRUE. Například následující kód vypíše a je větší než b, a se rovná b nebo a je menší než b:

if ($a > $b) {
    print "a je větší než b";
} elseif ($a == $b) {
    print "a se rovná b";
} else {
    print "a je menší než b";
}

V rámci jednoho příkazu if může být více příkazů elseif. Provádí se první příkaz elseif (pokud vůbec nějaký), jehož výraz je ohodnocen TRUE. V PHP můžete napsat i 'else if' (dvěma slovy), chování bude naprosto totožné jako u 'elseif' (jedním slovem). Syntaktický význam je mírně odlišný (znáte-li C, je to stejné), avšak ve výsledku dostaneme přesně totožné chování.

Příkaz elseif se provádí, pouze jsou-li příslušný (bezprostředně předcházející) výraz příkazu if a výrazy všech příslušných předcházejících příkazů elseif ohodnoceny jako FALSE, a konkrétní výraz v elseif ohodnocen jako TRUE.



add a note add a note User Contributed Notes
elseif
31-Jan-2007 02: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 09: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.

Alternativní syntaxe řídicích struktur> <else
Last updated: Sat, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites