Brian2000

Understanding Alternative syntax for control structures in PHP

Sometimes you wind up working on a page that switches back and forth between php and html. It can be tricky keeping track of code or making the mistake of over using the echo function. Luckily php supports an alternative syntax for control structures allowing you to jump in and out of php and html. Any code (html, php, anything) nested within a conditional will only parse if the defined conditions are matched. The end result is clean, organized, efficient code.

One of my interns once put all his html code into an array and printed each part while looping through function calls. It worked, but it his source code was so hard to read that when I asked him to take a look at it a few months later he couldn’t even make sense of what he wrote.

PHP's alternative control structure syntax makes it easy to identify code blocks and it’s widely used in many popular open source projects. WordPress for example makes use of this syntax all over the place. I you ever looked at the code inside a WordPress theme, I’m sure you’ve taken notice to this syntax. Here’s a quick example.

<?php// quick example, prints hungry if sandwich is false - it isn't
$sandwich = true;
if($sandwich == false):
  echo 'I am hungry.';
else:
  echo'Thank you, that was delicious.';
endif;
?>

Anyone familiar with Visual Basic might be drawing comparisons to the syntax and noticing the end if statement. Some might even suggest that this a counterproductive approach to clean code. PHP does this on purpose, because now you can jump out of the language and things can become a lot cleaner, and easy to read. So I’d sacrifice having to close an if statement with ‘endif’ any day over a complicated list of echo commands.

Supported Control Structures

PHP provides this alternative syntax for if, while, for, foreach, and switch statements. Any opening braces ({) you would normally use become a colon (:), and the closing brace is replaced by an endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. I included an example of each below.

<?php $a=1; $b=4;?>

----Example IF Statement
<?php if ($a == $b): ?> A equals B
<?php else:?> A does not equal B
<?php endif; ?>

----Example WHILE Statement
<?php while($a < $b): ?> Here is some code inside a while
<?php $a++; endwhile; ?>

----Example FOR Statement
<?php for($i = 1; $i <= 10; $i++):?> Some code in a for
<?php endfor; ?>

----Example SWITCH Statement
<?php switch($a): case 1:?> Code block for CASE 1
<?php break;?> <?php case 2: ?> Code block for CASE 2
<?php break;?> <?php case 3: ?> Code block for CASE 3
<?php break;?> <?php case 4: ?> Code block for CASE 4
<?php break;?> <?php endswitch; ?>

Common problem with the Switch statement

It is important to point out that switch statements written in this format require the first case to be included with the statement. If you put the first case in a separate PHP block, you will get the following error: Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_ENDSWITCH or T_CASE or T_DEFAULT I'm not sure exactly why PHP behaves this way, but it is a commonly made mistake that is not often explained or warned against.

THE RIGHT WAY
<?php switch($a): case 1:?> Code block for CASE 1
<?php break;?> <?php case 2: ?> Code block for CASE 2
<?php break;?> <?php case 3: ?> Code block for CASE 3
<?php break;?> <?php endswitch; ?>

THE WRONG WAY
<?php switch($a): ?> <?php case 1: ?> Code block for CASE 1
<?php break;?> <?php case 2: ?> Code block for CASE 2
<?php break;?> <?php case 3: ?> Code block for CASE 3
<?php break;?> <?php endswitch; ?>

Another Example

Here’s a more elaborate example which really highlights this point. Try switching the value of $user from true to false and see what happens.

 
<?php// This example displays two different blocks of html code depending the value of $user
$user = true;

if($user == true):?>
  

"welcome, You have logged in!"

<?php else:?> <?php endif; ?>

By now you’re a master of the alternative syntax. Perhaps a master of all things alternative, you’ll go on to start some alt-rock band now, and get a weird piercing or a tattoo. The point being, this is really easy syntax to understand and follow, it makes code easier to read, debug, and troubleshoot. Using this syntax is a win-win situation for any PHP developer. So, if you’re in the middle of writing an elaborate WordPress theme, or this is your first venture into the land of PHP, use this syntax and make your life easier!