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 ;?> is equal to one </td></tr></table>
<?php endif ; ?>
So the HTML code Won't excuted until the condition is true
Sintassi alternativa per le strutture di controllo
PHP offre una sintassi alternativa per alcune delle sue strutture di controllo; vale a dire, if, while, for, foreach e switch. Fondamentalmente la sintassi alternativa consiste nel sostituire la prima parentesi graffa con il carattere "duepunti" (:) e la seconda parentesi graffa con endif;, endwhile;, endfor;, endforeach;, oppure endswitch;, rispettivamente.
<?php if ($a == 5): ?>
a è uguale a 5
<?php endif; ?>
Nell'esempio precedente, il blocco HTML "a è uguale a 5" è incluso nel ramo if scritto utilizzando la sintassi alternativa. Il blocco HTML verrà visualizzato solamente se $a è uguale a 5.
La sintassi alternativa si applica anche ad else ed elseif. Nell'esempio che segue si mostra come utilizzare la sintassi alternativa nel caso di un if con elseif ed else:
<?php
if ($a == 5):
echo "a è uguale a 5";
echo "...";
elseif ($a == 6):
echo "a è uguale a 6";
echo "!!!";
else:
echo "a non è nè 5 nè 6";
endif;
?>
Sintassi alternativa per le strutture di controllo
07-Nov-2008 02:05
28-Jan-2008 02: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>
24-Oct-2007 10: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. ;-)
16-Oct-2007 11: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_;
27-Jun-2005 11: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.
12-Oct-2003 11: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
