While it’s not a great secret, many people still not aware of the alternative syntax for control structures under PHP.
The C-like syntax for control structures are very intuitive but can deal lots of trouble when your code has lot of pages and lots of ifs, fors, while and therefore lots of opening and closing braces.
NOTE: As remembered by jakyra on DZone, if you keep your code tidy you shouldn’t have functions that long and though not having this kind of problem, but this alternative syntax still have good benefits.

The alternative syntax
The alternative syntax consists in changing the opening brace ({) to a colon (:) and each closing brace to its respective closing function: endif;, endfor;, endforeach;, endwhile; and endswitch;.
Closing functions list
- opens
if ([parameters]): - closes
endif; - opens
for ([parameters]): - closes
endfor; - opens
foreach ([parameters]): - closes
endforeach; - opens
while ([parameters]): - closes
endwhile; - opens
switch ([value]): - closes
endswitch;
Benefits of this alternative syntax
The benefits of using this syntax are clear when put into practice. A far more readable code with no more commenting out braces like } // end :: if. You can instantly know which structural condition this closing brace belongs to and will no longer get confused between these and function braces.
Disadvantages of the alternative syntax
I personally do not consider this as a disadvantage but some people might consider since some text editors and IDEs have the ability to highlight the matching brace.
Examples
If
NOTE: else and elseif control structures follow the same structure.
// Standard syntax if ($variable === TRUE) { $otherVariable = "foo"; $aThirdVariable = "bar"; } elseif ($variable == "foo") { $otherVariable = "foo"; $aThirdVariable = "foo"; } else { $otherVariable = "bar"; $aThirdVariable = "foo"; } // end :: if
// Alternative syntax if ($variable === TRUE): $otherVariable = "foo"; $aThirdVariable = "bar"; elseif ($variable == "foo"): $otherVariable = "foo"; $aThirdVariable = "foo"; else: $otherVariable = "bar"; $aThirdVariable = "foo"; endif;
Foreach
$array = array("foo","bar"); // Standard syntax foreach ($array as $key => $value) { $foo = $key; $bar = $value; } // end :: foreach
$array = array("foo","bar"); // Alternative syntax foreach ($array as $key => $value): $foo = $key; $bar = $value; endforeach;
For
// Standard syntax for ( $i=0; $i<10; $i++ ) { $foo = $i; $bar = $i*10; print $foo+$bar; } // end :: for
// Alternative syntax for ( $i=0; $i<10; $i++ ): $foo = $i; $bar = $i*10; print $foo+$bar; endfor;
While
$i = 10; // Standard syntax while ($i > 0) { $foo = $i--; print $foo; } // end :: while
$i = 10; // Alternative syntax while ($i > 0): $foo = $i--; print $foo; endwhile;
Switch
// Standard syntax switch ($foo) { case "foo": $bar = FALSE; break; case "bar": default: $bar = TRUE; } // end :: switch
// Alternative syntax switch ($foo): case "foo": $bar = FALSE; break; case "bar": default: $bar = TRUE; endswitch;
Photo: Andy Fitz @ Flickr
Alternative syntax for PHP control structures | HEAVYWORKS…
While it’s not a great secret, many people still not aware of the alternative syntax for control structures under PHP….
abcphp.com
January 3, 2010 at 7:32 am
Kudos! What a neat way of thniknig about it.
Jonni
April 11, 2011 at 8:02 pm
Excellent post… I’ve always liked the alternative syntax for template scripts.
Brian Reich
January 4, 2010 at 10:13 am
[...] Alternative syntax for PHP control structures [...]
PHP 控制結構另一種語法 « Inet@Web
January 5, 2010 at 1:23 am
Don’t forget that at the beginning, PHP was used as a template engine :)
These alternative syntax take all their usefulness in a template.
cx42net
January 6, 2010 at 10:46 am
Just one question though. Have you made writing this blog as your profession or do you do this in your spare time?
Dominic Earles
February 21, 2010 at 12:59 pm
I know I’m just nit-picking, but your <acronym> tag says that PHP stands for “Pre-Hypertext Processing,” but it is actually a recursive acronym which stands for “PHP: Hypertext Preprocessor.”
Nick Bair
April 17, 2010 at 12:46 pm
@Nick Bair Thanks Nick! The acronym plugin data was outdated, now it’s correct!
Jan Seidl
April 17, 2010 at 1:40 pm
Stands back from the keboyard in amazement! Thanks!
Will
April 11, 2011 at 5:31 pm