search for in the  
<elseifwhile>
Last updated: Thu, 19 May 2005

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.



User Contributed Notes
Alternative syntax for control structures
groundzero at 0845 dot uk dot com
19-May-2005 03: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.
jasperbg at gmail dot com
31-Mar-2005 01:01
paul at example dot com, and all saying that $x ? $y : $z is an alternative structure for if...else:

It's not! It's a completely separate operator, a comparison operator called the Ternary operator. Look on this page:

http://php.net/language.operators.comparison
siebe-tolsma at home dot nl
18-Mar-2004 12: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 07: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 06: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 08: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 06: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

?>
christopher dot murtaghNO at mcgill dot SPAMca
16-May-2003 02:19
A comment to the above:

 You shouldn't need to change your code syntax because of nested flow control statements. Instead you should strongly think about your coding style and getting a better text editor. For example, indent your structures and sub structures:

<?php
 
if($foo){
   if(
$Bar){
      
// some code
  
}
   else{
      
// something else
  
}
}
else{
   for(
$i=0;$i<$foo;$i++){
      
// do something
  
}
}
?>

 Also, any text editor worth using for coding (emacs, vi(m), BBEdit, Elvis, etc..) should do bracket balancing in case you are really in deep.

<elseifwhile>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT