php Alternative syntax for control structures

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. (PHP 4, PHP 5)

<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>

In the above example, the HTML block "A is equal to 5" is nested within an if statement written in the alternative syntax. The HTML block would be displayed only if $a is equal to 5.

The alternative syntax applies to else and elseif as well. The following is an if structure with elseif and else in the alternative format:

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
?>

Note:

Mixing syntaxes in the same control block is not supported.

See also while, for, and if for further examples.

while><elseif/else if


[edit] Last updated: Fri, 06 Apr 2012

reject note add a note add a noteUser Contributed Notes Alternative syntax for control structures

temec987 at gmail dot com 02-Oct-2011 07:00

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!

dmgx dot michael at gmail dot com 19-Jul-2010 08:42

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; ?>

ej at iconcept dot fo 28-May-2009 05:30

if statement in 1 line
<?php
$hour = 11;
print $foo = ($hour < 12) ? "Good morning!" : "Good afternoon!";
?>
return Good morning!

flyingmana 26-Mar-2009 01:43

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

SM 03-Feb-2009 05:12

The control structure should be like in BASIC languages:
<?php
if ($a == 12):
  echo "a is 12\n";
end if;
while (true):
  echo "loop loop loop\n";
end while;
?>
or just use end operator like in Ruby
<?php
if ($a == 12):
  echo "a is 12\n";
end;
while (true):
  echo "loop loop loop\n";
end;
?>

mido_alone2001 at yahoo dot com 07-Nov-2008 06:05

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

Leave a comment