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.';
  echo'Thank you, that was delicious.';

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;?>
<p>----Example IF Statement<br>
<?php if ($a == $b): ?>
A equals B<br>
<?php else:?>
A does not equal B<br>
<?php endif; ?></p>
<p>----Example WHILE Statement<br>
<?php while($a < $b): ?>
Here is some code inside a while<br>
endwhile; ?>
<p>----Example FOR Statement<br>
<?php for($i = 1; $i <= 10; $i++):?>
Some code in a for<br>
<?php endfor; ?>
<p>----Example SWITCH Statement<br>
<?php switch($a): case 1:?>
    Code block for CASE 1<br>
    <?php break;?>
    <?php case 2: ?>
    Code block for CASE 2<br>
    <?php break;?>
    <?php case 3: ?>
    Code block for CASE 3<br>
    <?php break;?>
    <?php case 4: ?>
    Code block for CASE 4<br>
    <?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.

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):?>
  <div class="user_message">
  <h2>"welcome, You have logged in!"</h2>
 <?php else:?>
  <div class="sign_in">
   Please sign in.
<?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!

5 responses to “Understanding Alternative syntax for control structures in PHP”

  1. I truly appreciate this post. I’ve been looking all over for this!

    You’ve made my day! Thank you again!

    I have been struggling all day long trying to get a switch statement to work. This is the only site with the right answer/syntax!!!

  3. Thanks you so much.. I have been stuck for 4 days to get switch statement to work. Now my problem is solved.. Thank again.