suche nach in der

while> <elseif/else if
Last updated: Fri, 03 Sep 2010

view this page in

Alternative Syntax für Kontrollstrukturen

PHP bietet eine alternative Syntax für einige seiner Kontrollstrukturen an, namentlich für if, while, for, foreach und switch. In jedem Fall ist die Grundform der alternativen Syntax ein Wechsel der öffnenden Klammer gegen einen Doppelpunkt (:) und der schließenden Klammer in endif;, endwhile, endfor;, endforeach; respektive endswitch.

<?php if ($a == 5): ?>
A ist gleich 5
<?php endif; ?>

Im obigen Beispiel ist der HTML-Block "A ist gleich 5" in ein if-Statement verschachtelt, das in alternativer Syntax notiert ist. Der HTML-Block würde nur angezeigt werden, wenn $a gleich 5 ist.

Die alternative Syntax lässt sich ebenfalls auf else und elseif anwenden. Im Folgenden wird eine if-Struktur mit elseif- und else-Teilen im alternativen Format gezeigt:

<?php
if ($a == 5):
    echo 
"a gleich 5";
    echo 
"...";
elseif (
$a == 6):
    echo 
"a gleich 6";
    echo 
"!!!";
else:
    echo 
"a ist weder 5 noch 6";
endif;
?>

Hinweis:

Das Vermischen unterschiedlicher Schreibweisen im selben Kontrollblock ist nicht unterstützt.

Siehe auch while, for und if für weitere Beispiele.



add a note add a note User Contributed Notes
Alternative Syntax für Kontrollstrukturen
oly at czo dot net
27-Jun-2007 10:59
3 Lines to make table row colors alternate.  Could be used for other applications as well.

<?
//1. Initialize alternating variable.
$alt_color=true;

while(
$row=mysql_fetch_array($result)){
 
//2. If alt var true set one color if false set the other.
 
echo (($alt_color) ? "<tr bgcolor=\"#ffffff\">\n" : "<tr bgcolor=\"#D9E5F1\">\n");
  echo
"<td>echo $row['id'];;</td>\n";
  echo
"</tr>\n";
 
//3. Alternate the variable value;
 
(($alt_color) ? $alt_color=false : $alt_color=true);
}
?>
geekman at Textbook Torrents dot com
01-Apr-2007 12:09
The bug in your example is user error.

<?
$foobar
= 2;

echo
'Foobar is ' . ($foobar == 2) ? 'foo' : 'bar';
// outputs 'foo';

echo 'Foobar is ' . (($foobar == 2) ? 'foo' : 'bar');
// outputs 'Foobar is foo';
?>

When using the ? : operators you should always wrap the entire thing in parentheses to avoid the problem you describe. By my understanding, the issue an extension of your example:

<?
$pet
-type = 1;
$pet-name = 'Mittens';

echo
'My ' . ($pet-type == 1) ? 'cat' : 'puppy' . 'dog' . '\'s name is ' . $pet-name . '.';
// returns 'cat'

echo 'My ' . (($pet-type == 1) ? 'cat' : 'puppy' . 'dog') . '\'s name is ' . $pet-name . '.';
// returns 'My cat\'s name is Mittens.'
?>

You see that without parentheses the parser has no way of differentiating between the false case and the rest of the string.
fernandoleal at dragoncs dot com
03-Feb-2007 08:17
If you need nested ifs on I var its important to group the if so it works.
Example:
<?php
//Dont Works
//Parse error: parse error, unexpected ':'
 
$var='<option value="1" '.$status == "1" ? 'selected="selected"' :''.'>Value 1</option>';
 
//Works:
 
$var='<option value="1" '.($status == "1" ? 'selected="selected"' :'').'>Value 1</option>';

echo
$var;
?>
atw
04-Nov-2006 01:47
As "qbolec" states, all the notes below which use the "?" are actually references to the ternary operator (http://uk.php.net/operators.comparison").

Whilst this is kind of relevent, it probably shouldn't be in this section.
Neil
29-Sep-2006 09:08
as skippy noted above this is very useful stuff for interspersed php & html. Here is alternative syntax example using a bitwise comparison to set checkboxes on and off to prefill a permissions bitmask calculator form:
<?
$bitmask
= 481683;
?>
.... [yada yada yada]....
<input type="checkbox" name="SOME_PERMISSION_VARIABLE" value="32768" <? echo($bitmask & 32768 ? "checked" : ""); ?>>Can perform some operation<br/>
<input type="checkbox" name="SOME_OTHER_PERMISSION_VARIABLE" value="65536" <? echo($bitmask & 65536 ? "checked" : ""); ?>>Can perform some other operation<br/>

You supply a bitmask, and it prechecks all the permissions the user has so you don't need to remember what they already have to recheck them all individually.

It's much more elegant than traditional if's.
davidforest at gmail dot com
20-Oct-2005 03:26
If you need a tidy way to do a lot of condition testing, switch statement will do the job well:

switch (true){

    case ($a>0):
                     //do sth;
                     break;
    case ($b>0):
                     //do sth;
                     break;
    case ($c>0):
                     //do sth;
                     break;
    case ($d>0):
                     //do sth;
                     break;

}
skippy at zuavra dot net
27-Jun-2005 01:32
If it needs saying, this alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them.

Interface templates are very often in need of this, especially since the PHP code in them is usually written by one person (who is more of a programmer) and the HTML gets modified by another person (who is more of a web designer). Clear separation in such cases is extremely useful.

See the default templates that come with WordPress 1.5+ (www.wordpress.org) for practical and smart examples of this alternative syntax.
siebe-tolsma at home dot nl
18-Mar-2004 07:22
As a rection on sttoo, if you use nested if's a bit different they are less likely to cause mistakes:
[EDITOR'S NOTE: Referenced Note has been removed]

<?php
$one
= true;
$two = true;

$result = ($one ? "one" : ($two ? "two" : "none"));    // $result is "one"

$one = false;
$result = ($one ? "one" : ($two ? "two" : "none"));    // $result is "two"

$two = false;
$result = ($one ? "one" : ($two ? "two" : "none"));    // $result is "none"

?>
i a m 4 w e b w o r k at hotmail dot com
13-Oct-2003 01:38
Good tutorial on using alternative control structure syntax at:
http://www.onlamp.com/pub/a/php/2001/05/03/php_foundations.html?page=1
paul at example dot com
06-Sep-2003 03:27
There is an other alternative syntax:

<?php
if ($a > 5) {
    echo
"big";
} else {
    echo
"small";
}
?>

can be replaced by:

<?php
echo $a > 5 ? "big" : "small";
?>

while> <elseif/else if
Last updated: Fri, 03 Sep 2010