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>
Алтернативен синтаксис за контролни структури
PHP предоставя алтернативен синтаксис за някои от контролните структури, а именно: if, while, for, foreach и switch. Във всички случаи, основната форма на алтернативния синтаксис се получава като се смени отварящата фигурна скоба на двоеточие и затварящата къдрава скоба съответно на endif;, endwhile;, endfor;, endforeach; или endswitch;.
<?php if ($a == 5): ?>
A е равно на 5
<?php endif; ?>
В горния пример, HTML блокът "A е равно на 5" е вложен в конструкция if, написана с алтернативния синтаксис. HTML блокът ще бъде изведен само ако $a е равна на 5.
Алтернативният синтаксис важи също и за else и elseif. Следващият пример представлява структурата if с elseif и else в алтернативен формат:
<?php
if ($a == 5):
echo "a е равно на 5";
echo "...";
elseif ($a == 6):
echo "a е равно на 6";
echo "!!!";
else:
echo "a не е нито 5, нито 6";
endif;
?>
Алтернативен синтаксис за контролни структури
jeremia at gmx dot at
28-Jan-2008 03:52
28-Jan-2008 03:52
php dot net at eoasys dot com
25-Oct-2007 12:03
25-Oct-2007 12:03
In response to spa:
Yeah, that's for sure! Seems so obvious, but remains a tough sell... I avoid the "Bracket Racket", and use it only where the (ahem) "Clearer Syntax" wasn't implemented.
A further improvement would be a "Noun-Verb" form of end structures. Such as:
if (...):
while (...):
...
if (...):
...
...
ifend;
...
whileend;
ifend;
This would make it yet another level of easier to tell which block end you're looking at. ;-)
spa
17-Oct-2007 01:40
17-Oct-2007 01: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-Jun-2005 01:32
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.
i a m 4 w e b w o r k at hotmail dot com
13-Oct-2003 01:38
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
