PHP Doku:: Alternative Syntax für Kontrollstrukturen - control-structures.alternative-syntax.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzKontrollstrukturenAlternative Syntax für Kontrollstrukturen

Ein Service von Reinhard Neidl - Webprogrammierung.

Kontrollstrukturen

<<elseif/else if

while>>

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.


8 BenutzerBeiträge:
- Beiträge aktualisieren...
dmgx dot michael at gmail dot com
19.07.2010 17:42
If you follow MVC design pattern then only your view files should have HTML in them to begin with.  Using the braceless syntax in these files only further separates them thematically from the rest of the code.

The major advantage of braceless syntax is that braces get lost while jumping into and out of php mode, especially if you use php short tags (which contrary to what is stated elsewhere, if you are using htaccess to deploy mod_rewrite in your application it is safe to use short tags in your application.  The server admins CANNOT deny short tags to you while simultaneously granting mod_rewrite (and why they would even try is beyond me).

Another thing I've noted in the examples above - it is safe to omit the ending semicolon prior to a script close tag, and it's slightly easier to read. <? endforeach ?> than <?php endforeach; ?>
ej at iconcept dot fo
28.05.2009 14:30
if statement in 1 line
<?php
$hour
= 11;

print
$foo = ($hour < 12) ? "Good morning!" : "Good afternoon!";

?>
return Good morning!
flyingmana
26.03.2009 10:43
It seems to me, that many people think that

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

is only with alternate syntax possible, but

<?php if ($a == 5){ ?>
A ist gleich 5
<?php }; ?>

is also possible.

alternate syntax makes the code only clearer and easyer to read
SM
4.02.2009 2:12
The control structure should be like in BASIC languages:
<?php
if ($a == 12):
  echo
"a is 12\n";
end if;

while (
true):
  echo
"loop loop loop\n";
end while;
?>

or just use end operator like in Ruby
<?php
if ($a == 12):
  echo
"a is 12\n";
end;

while (
true):
  echo
"loop loop loop\n";
end;
?>
mido_alone2001 at yahoo dot com
7.11.2008 15:05
Hello , when you going to make a script , you must try easist way to do and fastest way to parse ..
using alternative-syntax is very useful to shorten your code
e.g :
If you want to do:

<?php
    $a
=1 ;
if (
$a==1) {
     echo
"<table border=1><tr><td>$a is equal to one    </td></tr></table> " ;
}
?>

You can do it using alternative-syntax as following :

<?php
    $a
=1 ;
if (
$a==1) :?>
<table border=1><tr><td><?echo $a ;?> &nbsp;is equal to one </td></tr></table>
<?php endif ; ?>

So the HTML code Won't excuted until the condition is true

[EDIT BY danbrown AT php DOT net: Contains a bug fix provided by (gmdebby AT gmail DOT com).]
jeremia at gmx dot at
28.01.2008 15:52
If you wan't to use the alternative syntax for switch statements this won't work:

<div>
<?php switch($variable): ?>
<?php
case 1: ?>
<div>
Newspage
</div>
<?php break;?>
<?php
case 2: ?>
</div>
Forum
<div>
<?php break;?>
<?php
endswitch;?>
</div>

Instead you have to workaround like this:

<div>
<?php switch($variable):
case
1: ?>
<div>
Newspage
</div>
<?php break;?>
<?php
case 2: ?>
</div>
Forum
<div>
<?php break;?>
<?php
endswitch;?>
</div>
spa
17.10.2007 1:40
[EDITOR'S NOTE: reference to deleted note removed]

The end_; structure sometimes makes it easier to tell which block statement end you are looking at.  It's much harder to tell which nested block a } belongs to than an end_;
skippy at zuavra dot net
27.06.2005 13: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.



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",...)