Techblog Index

Alternative syntax for PHP control structures

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

 
 

Reader's thoughts on "Alternative syntax for PHP control structures"

9
  1. 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….

  2. Excellent post… I’ve always liked the alternative syntax for template scripts.

  3. [...] Alternative syntax for PHP control structures [...]

  4. Don’t forget that at the beginning, PHP was used as a template engine :)
    These alternative syntax take all their usefulness in a template.

  5. Just one question though. Have you made writing this blog as your profession or do you do this in your spare time?

  6. 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.”

  7. @Nick Bair Thanks Nick! The acronym plugin data was outdated, now it’s correct!

Leave a Reply