PHP Doku:: else - control-structures.else.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzKontrollstrukturenelse

Ein Service von Reinhard Neidl - Webprogrammierung.

Kontrollstrukturen

<<if

elseif/else if>>

else

Oft will man eine Anweisung ausführen wenn eine bestimmte Bedingung erfüllt ist und eine andere Anweisung wenn dies nicht der Fall ist. Dies ist der Einsatzzweck von else. else erweitert eine if-Anweisung um eine weitere Anweisung die dann ausgeführt werden soll wenn der Ausdruck in der if-Anweisung zu FALSE ausgewertet wird. Der folgende Programmkode würde z.B. a ist größer als b ausgeben wenn $a größer als b ist, ansonsten a ist NICHT größer als b

<?php
if ($a $b) {
  echo 
"a ist größer als b";
} else {
  echo 
"a ist NICHT größer als b";
}
?>
Die else Anweisung wird nur dann ausgeführt wenn der if Ausdruck und alle etwaigen elseif als FALSE ausgewertet wurden (siehe auch elseif).


6 BenutzerBeiträge:
- Beiträge aktualisieren...
smoldar at gmail dot com
28.10.2010 16:21
You can use the if to make a Yes/No field, verify if the statement is real or not and show the correct option checked.

<?php if($variable == 'S') {?>
<input name="blah" type="radio" value="Y" checked="checked"> Yes
<input name="blah" type="radio" value="N"> No
<?php } else {?>
<input name="blah" type="radio" value="Y"> Yes
<input name="blah" type="radio" value="N" checked="checked"> No
<?php }?>
Larry H-C
17.08.2009 19:08
When you escape out of HTML, you can get an UNEXPECTED T_ELSE error with the following:

Error:

<? if( $condition ) {
       
dosomething;
   }
?>

<? else {
      
dosomethingelse;
   }
?>

Correct:

<? if( $condition ) {
      
dosomething;
?>

<? } else {
      
dosomethingelse;
   }
?>

Apparently the compiler thinks a ?> <? breaks the connection between the } and the else
Theoden
25.04.2008 2:42
At Caliban Darklock

I don't know if it is just improvements in the parser, but there is a negligible difference in the performance of "elseif" vs "else if" as of version 5. One thousandth of a second in your example and 8 thousandths if the eval statement is repeated 5 times.
If the constructs are in regular code, then there appears to be no difference. This leads me to believe that the difference in the eval code is from there being an extra parser token.

Also the main performance burden of recursive functions is the stack operations of changing the context. In this case I believe that it would parse to very similar (if not identical) jmp controls.

In summary, use your preference. Readability and maintainability rank far higher on the priority scale.

One Additional note, there appears to be a limit of the number of "else if" statements (perhaps nested statements in general) that php will handle before starting to get screwy. This limit is about 1100. "elseif" is not affected by this.
dormeydo at gmail dot com
12.04.2008 13:51
An alternative and very useful syntax is the following one:

statement ? execute if true : execute if false

Ths is very usefull for dynamic outout inside strings, for example:

print('$a is ' . ($a > $b ? 'bigger than' : ($a == $b ? 'equal to' : 'smaler than' )) .  '  $b');

This will print "$a is smaler than $b" is $b is bigger than $a, "$a is bigger than $b" if $a si bigger and "$a is equal to $b" if they are same.
mitch at mitchellbeaumont dot com
24.07.2007 9:09
At gwmpro at yahoo dot com

The curly brace is not required however, for readability and maintenance, many developers would consider it bad style not to include them.
Caliban Darklock
8.11.2004 20:24
If you're coming from another language that does not have the "elseif" construct (e.g. C++), it's important to recognise that "else if" is a nested language construct and "elseif" is a linear language construct; they may be compared in performance to a recursive loop as opposed to an iterative loop.

<?php
$limit
=1000;
for(
$idx=0;$idx<$limit;$idx++) 
{
$list[]="if(false) echo \"$idx;\n\"; else"; }
$list[]=" echo \"$idx\n\";";
$space=implode(" ",$list);| // if ... else if ... else
$nospace=implode("",$list); // if ... elseif ... else
$start=array_sum(explode(" ",microtime()));
eval(
$space);
$end=array_sum(explode(" ",microtime()));
echo
$end-$start . " seconds\n";
$start=array_sum(explode(" ",microtime()));
eval(
$nospace);
$end=array_sum(explode(" ",microtime()));
echo
$end-$start . " seconds\n";
?>

This test should show that "elseif" executes in roughly two-thirds the time of "else if". (Increasing $limit will also eventually cause a parser stack overflow error, but the level where this happens is ridiculous in real world terms. Nobody normally nests if() blocks to more than a thousand levels unless they're trying to break things, which is a whole different problem.)

There is still a need for "else if", as you may have additional code to be executed unconditionally at some rung of the ladder; an "else if" construction allows this unconditional code to be elegantly inserted before or after the entire rest of the process. Consider the following elseif() ladder:

<?php
if($a) { conditional1(); }
elseif(
$b) { conditional2(); }
elseif(
$c) { conditional3(); }
elseif(
$d) { conditional4(); }
elseif(
$e) { conditional5(); }
elseif(
$f) { conditional6(); }
elseif(
$g) { conditional7(); }
elseif(
$h) { conditional8(); }
else {
conditional9(); }
?>

To insert unconditional preprocessing code for $e onward, one need only split the "elseif":

<?php
if($a) { conditional1(); }
elseif(
$b) { conditional2(); }
elseif(
$c) { conditional3(); }
elseif(
$d) { conditional4(); }
else {
....
unconditional();
....if(
$e) { conditional5(); }
....elseif(
$f) { conditional6(); }
....elseif(
$g) { conditional7(); }
....elseif(
$h) { conditional8(); }
....else {
conditional9(); }
}
?>

The alternative is to duplicate the unconditional code throughout the construct.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)