PHP’s alternative syntax and why it’s useful
PHP offers an alternative way to write their control structures for as long as I’ve remembered. It basically does away with the curly brackets and replaces the opening curly with a colon (:) and the closing with ‘end’-whatever. I have to be honest and say I’ve never really found a need to use the alternative syntax, simply because I’m so used to using the curly braces after so many years of using PHP. Plus, it’s less typing!
But having spend time working on CodeIgniter projects, I have found myself adopting this alternative syntax when it comes to buidling the CI’s views templates. I appreciate that there is place in the PHP universe for this alternative coding style.
The best time to use this alternative style is when you need to mix PHP codes with HTML. In CI, this normally happens in views. Have a look at the IF example below of a typical HTML table layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <table> <tr> <td>Site Name</td> <td>Site URL</td> <td>Site Owner</td> </tr> <?php foreach ($sites as $site) { ?> <?php if ($site['name'] == 'AskAboutPHP') { ?> <tr> <td><?php echo $site['name'];?></td> <td><?php echo $site['url'];?></td> <td><?php echo $site['owner'];?></td> </tr> <?php } else { ?> <tr> <td> - </td> <td> - </td> <td> - </td> </tr> <?php } ?> <?php } ?> </table> |
The same table can be written in the alternative style as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <table> <tr> <td>Site Name</td> <td>Site URL</td> <td>Site Owner</td> </tr> <?php foreach ($sites as $site): ?> <?php if ($site['name'] == 'AskAboutPHP') : ?> <tr> <td><?php echo $site['name'];?></td> <td><?php echo $site['url'];?></td> <td><?php echo $site['owner'];?></td> </tr> <?php else: ?> <tr> <td> - </td> <td> - </td> <td> - </td> </tr> <?php endif; ?> <?php endforeach; ?> </table> |
Personally, I find the alternative syntax gels better with HTML even though it’s more verbose. But because this form is more verbose, if a HTML developer was to look at this, I would imagine the second example would make more sense and easier to read and understand than the first table.
This alternative form works for PHP’s ‘if’, ’switch’, ‘while’, ‘for’ and ‘foreach’ control statements.
See Everything PHP’s article on Alternative Syntax for Control Structures for more examples.
Alternative short form ‘if-else’ statement
There is also another shorter way of writing the ‘if-else’ statements which I also found very useful in my CI views. Take for example the following:
1 2 3 4 5 6 7 | <?php if (!empty($a)) { $a = TRUE; } else { $a = FALSE; } ?> |
This can be written as:
1 2 3 | <?php $a = (!empty($a)) ? TRUE : FALSE; ?> |
The portion before the ‘?’ is the condition, followed by the ‘true’ outcome, then colon, then the ‘false’ outcome.
This helps to keep the coding of ‘if-else’ to the minimum, especially when you have to perform a lot of validation type check which could be a whole stack of ‘if-else’ statements.
In: PHP Tutorials · Tagged with: codeigniter, PHP, syntax

Permalink
Really appreciate this post. Just got finished doing a large piece of javascript magic with PHP and I must say the normal bracket notation just doesn’t cut it for good reading.
Permalink
I can see the argument for the alternative syntax for the control structures making more sense in views, although my own feeling is that to the inexperienced programmer it looks like a different language.
However the ternary operator is just bad news and far less readable. It should be avoided at all costs. Especially as some genius will come along and think “hey! I can nest these!”. Code should be self describing and easy to read. what ? is : this just isn’t.
Permalink
And now we are at it. Enable shorttags on your server and USE them. I know a lot of stupid dry theoryanals dislikes them, but is faster to write and a lot more pretty. Shorttags will still be there in PHP6 and they solve more problems (ie. no template language needed) than they create (problem writing xml tag). So stop being anal and write with shorttags, the new best practice for PHP
Permalink
more simple
$a = !empty($a);
Permalink
[...] the “Ask About PHP” blog today there’s a new post looking at some of the alternative syntaxes that PHP has to offer for control [...]
Permalink
when I hack large template-ish html/php spaghetties, I use a simple editor, called PSPad (because sucky code and sucky IDE-s together suck just way too much for my taste – for not sucky code I use Netbeans). My little editor knows where a pair of brackets starts/ends, and highlights the other end, and I think there will not be support for it ever. So I stay with the brackets.
And short tags ftw.
Permalink
[...] Excerpt from: PHP’s alternative syntax and why it’s useful | Ask About PHP [...]
Permalink
You can also use short tags :
Permalink
@#3,
And you think problems writing a XML tag is good?
Permalink
more simple
$a = !!$a;
Permalink
I dabble in CodeIgniter but I have to admit that I don’t really like writing alternative syntax for PHP, but because of your wonderful explanation and example, it made much more sense to me now. Wordpress template files also uses this kind of syntax. Nice article. Thank you.
Permalink
One thing to note is that the one row short function (not having to use brackets) does not work with this alternative syntax.
If you have an if statement with only one row to execute, leaving out the { }, with the original syntax you can still have an else statement after that and use brackets, but if you first have an if statement with neither brackets nor : your ‘else:’ statement will break.
Permalink
PHP’s alternative syntax and why it’s useful | Ask About PHP great article thank you.
Permalink
PHP’s alternative syntax and why it’s useful | Ask About PHP great article thank you.
Permalink
agree,
alternative syntax realy useful when the code mixed with others code, such as JavaScript or HTML.
it made the code more readable and clear.
Permalink
So i guest we don’t need templating system anymore huh?