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

search for in the

while> <elseif
Last updated: Fri, 22 Aug 2008

view this page in

Sintaxis Alternativa de Estructuras de Control

PHP ofrece una sintaxis altenativa para alguna de sus estructuras de control; a saber, if, while, for, y switch. En cada caso, la forma básica de la sintaxis alternativa es cambiar abrir-llave por dos puntos (:) y cerrar-llave por endif;, endwhile;, endfor;, or endswitch;, respectivamente.

<?php if ($a==5): ?>
 A es igual a 5
 <?php endif; ?>

En el ejemplo de arriba, el bloque HTML "A es igual 5" se anida dentro de una sentencia if escrita en la sintaxis alternativa. El bloque HTML se mostraría solamente si $a fuera igual a 5.

La sintaxis alternativa se aplica a else y también a elseif. La siguiente es una estructura if con elseif y else en el formato alternativo:

<?php
 
if ($a == 5):
     print 
"a es igual a 5";
     print 
"...";
 elseif (
$a == 6):
     print 
"a es igual a 6";
     print 
"!!!";
 else:
     print 
"a no es ni 5 ni 6";
 endif;
?>

Mirar también while, for, e if para más ejemplos.



while> <elseif
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
Sintaxis Alternativa de Estructuras de Control
huskyr at gmail dot com
06-Oct-2008 04:42
Note that the alternative syntax might not work if short_open_tag is set to 'off' in your php.ini file.
jeremia at gmx dot at
28-Jan-2008 06: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>
php dot net at eoasys dot com
24-Oct-2007 03: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
16-Oct-2007 04: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 04: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
12-Oct-2003 04: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

while> <elseif
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites