downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

while> <elseif/else if
[edit] Last updated: Fri, 17 May 2013

view this page in

Sintassi alternativa per le strutture di controllo

(PHP 4, PHP 5)

PHP offre una sintassi alternativa per alcune delle sue strutture di controllo, ovvero: if, while, for, foreach, e switch. In ognuno di questi casi, l'approccio di base della sintassi alternativa è quello di cambiare la parentesi graffa aperta con il segno di duepunti (:) e la parentesi graffa chiusa rispettivamente con endif;, endwhile;, endfor;, endforeach;, o endswitch;.

<?php if ($a == 5): ?>
A è uguale a 5
<?php endif; ?>

Nell'esempio presentato, il blocco HTML "A è uguale a 5" è inserito all'interno in un blocco if scritto con la sintassi alternativa. Il blocco HTML sarà visualizzato solo se $a è uguale a 5.

La sintassi alternativa si applica nello stesso modo anche ad else e ad elseif. Quanto segue è un esempio della struttura if con sezioni elseif ed else nel formato alternativo:

<?php
if ($a == 5):
    echo 
"a è uguale a 5";
    echo 
"...";
elseif (
$a == 6):
    echo 
"a è uguale a 6";
    echo 
"!!!";
else:
    echo 
"a è diverso sia da 5 che da 6";
endif;
?>

Nota:

L'utilizzo delle due sintassi all'interno dello stesso blocco non è supportato.

Vedere anche while, for, ed if per ulteriori esempi.



while> <elseif/else if
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes Sintassi alternativa per le strutture di controllo - [7 notes]
up
6
skippy at zuavra dot net
7 years ago
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.
up
13
flyingmana
4 years ago
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
up
11
temec987 at gmail dot com
1 year ago
A simple alternative to an if statement, which is almost like a ternary operator, is the use of AND. Consider the following:

<?php
     $value
= 'Jesus';

    
// This is a simple if statement
    
if( isset( $value ) )
     {
          print
$value;
     }

     print
'<br />';

    
// This is an alternative
    
isset( $value ) AND print( $value );
?>

This does not work with echo() for some reason. I find this extremely useful!
up
5
ej at iconcept dot fo
4 years ago
if statement in 1 line
<?php
$hour
= 11;

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

?>
return Good morning!
up
2
jeremia at gmx dot at
5 years ago
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>
up
-5
dmgx dot michael at gmail dot com
2 years ago
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; ?>
up
-10
mido_alone2001 at yahoo dot com
4 years ago
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).]

 
show source | credits | stats | sitemap | contact | advertising | mirror sites