PHP                
            Alternative Syntax für Kontrollstrukturen
        
        
            
    Suche…
Syntax
- Struktur: / * Code * / Endstruktur;
 
Bemerkungen
 Beim Mischen der alternativen Struktur für den switch mit HTML ist es wichtig, dass zwischen dem ursprünglichen switch($condition): und dem ersten case $value: kein Leerzeichen angezeigt wird. Dabei wird versucht, vor einem Fall etwas (Whitespace) zu wiederholen. 
 Alle Kontrollstrukturen folgen der gleichen Grundidee. Anstatt endstructure; Klammern zu verwenden, um den Code einzukapseln, verwenden Sie einen Doppelpunkt und eine endstructure; Anweisung: structure: /* code */ endstructure; 
Alternative für Aussage
<?php
for ($i = 0; $i < 10; $i++):
    do_something($i);
endfor;
?>
<?php for ($i = 0; $i < 10; $i++): ?>
    <p>Do something in HTML with <?php echo $i; ?></p>
<?php endfor; ?>
        Alternative while-Anweisung
<?php
while ($condition):
    do_something();
endwhile;
?>
<?php while ($condition): ?>
    <p>Do something in HTML</p>
<?php endwhile; ?>
        Alternative foreach-Anweisung
<?php
foreach ($collection as $item):
    do_something($item);
endforeach;
?>
<?php foreach ($collection as $item): ?>
    <p>Do something in HTML with <?php echo $item; ?></p>
<?php endforeach; ?>
        Alternative switch-Anweisung
<?php
switch ($condition):
    case $value:
        do_something();
        break;
    default:
        do_something_else();
        break;
endswitch;
?>
<?php switch ($condition): ?>
<?php case $value: /* having whitespace before your cases will cause an error */ ?>
    <p>Do something in HTML</p>
    <?php break; ?>
<?php default: ?>
    <p>Do something else in HTML</p>
    <?php break; ?>
<?php endswitch; ?>
        Alternative if / else-Anweisung
<?php
if ($condition):
    do_something();
elseif ($another_condition):
    do_something_else();
else:
    do_something_different();
endif;
?>
<?php if ($condition): ?>
    <p>Do something in HTML</p>
<?php elseif ($another_condition): ?>
    <p>Do something else in HTML</p>
<?php else: ?>
    <p>Do something different in HTML</p>
<?php endif; ?>
    
    
    
    
    Modified text is an extract of the original Stack Overflow Documentation
        Lizenziert unter CC BY-SA 3.0
        Nicht angeschlossen an Stack Overflow