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

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



Alternative syntax for control structures
08-May-2006 02:51
matheo's example is the classic act of using a hammer to set a screw.  (No offense...)

As programmers, we <b>must</b> become comfortable that the result of a logic-expression <i>is a value</i> just like any other.

Therefore instead of:

<?php
$a
= 1 ;
$b = 2 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo
$a_is_bigger ;
# returns 0
?>

Use this:

<?php
$a
= 1;
$b = 2;
$a_is_bigger = $a > $b;
...
?>

You read it as "variable $a_is_bigger is set to the result of the comparison of $a to $b".  Compare that to the read-thru of the upper example: "variable $is_a_bigger is set to true if the is-greater comparison of $a to $b is true, and set to false if the comparison is false."  How redundant.  The only thing more redundant is the ever increasingly popular 'duh' logic of today's <i>modern</i> PHP programming:

<?php
   $a
= 1;
  
$b = 2;
   if(
$a > $b )
   {
      
$a_is_bigger = true ;
   }
   else if(
$b >= $a )
   {
      
$a_is_bigger = false ;
   }
?>

8 lines to do what one simple construction can do ... which if we <i>learn to recognize it</i> vastly speeds the reading and appreciation of the purpose of the arbitrary algorithm.

The <b>only constructive criticism</b> that I've heard about the use of both ternary operators and logic-evaluation-direct-assign is that neither of them are easy to debug by adding 'prints' or 'echos' to their interiors.  Point well made.  Yet, if they are as simple as the above examples, it must be argued that the use of 8 lines to accomplish what a single comparison-assignment could do may well have resulted in more potential errors.  (For instance, did I remember to put the '=' in the second comparison as the exact compliment of the first?)

rlynch AT lynchmarks DOT com
davidforest at gmail dot com
19-Oct-2005 06:26
If you need a tidy way to do a lot of condition testing, switch statement will do the job well:

switch (true){

   case ($a>0):
                     //do sth;
                     break;
   case ($b>0):
                     //do sth;
                     break;
   case ($c>0):
                     //do sth;
                     break;
   case ($d>0):
                     //do sth;
                     break;

}
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.
groundzero at 0845 dot uk dot com
19-May-2005 01:04
Person below...

paul at example dot com didnt say its was a replacement, or exactly the same as the if statement. He just gave a piece of code that, in this case had the same effect.
siebe-tolsma at home dot nl
18-Mar-2004 10:22
As a rection on sttoo, if you use nested if's a bit different they are less likely to cause mistakes:

<?php
$one
= true;
$two = true;

$result = ($one ? "one" : ($two ? "two" : "none"));    // $result is "one"

$one = false;
$result = ($one ? "one" : ($two ? "two" : "none"));    // $result is "two"

$two = false;
$result = ($one ? "one" : ($two ? "two" : "none"));    // $result is "none"

?>
ssttoo at hotmail dot com
05-Dec-2003 05:20
Nested ternary operators can be used, although not a "good programming practice", as it does not promote readability and is prone to errors.

<?php
$one
= true;
$two = true;
$result = ($one) ? "one" : (($two) ? "two" : "none");    // $result is "one"
$result = ($one) ? "one" : ($two) ? "two" : "none";      // $result is "two"
?>
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
paul at example dot com
06-Sep-2003 06:27
There is an other alternative syntax:

<?php
if ($a > 5) {
   echo
"big";
} else {
   echo
"small";
}
?>

can be replaced by:

<?php
echo $a > 5 ? "big" : "small";
?>
matheo at step dot polymtl dot ca
04-Sep-2003 04:37
You can use a short syntax for "if"
$a = (condition) ? value if condition is true : value if condition is false;

<?php
$a
= 1 ;
$b = 2 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo
$a_is_bigger ;
# returns 0

$a = 2 ;
$b = 1 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo
$is_a_bigger ;
# returns 1

?>

<elseifwhile>
 Last updated: Tue, 15 Nov 2005