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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on April 16, 2009 at 2:34 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , ,

16 Responses

Subscribe to comments via RSS

  1. Written by Mike on April 16, 2009 at 9:59 pm
    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.

  2. Written by Alex Mace on April 16, 2009 at 10:17 pm
    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.

  3. Written by bloky on April 16, 2009 at 10:18 pm
    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 :)

  4. Written by daremar on April 17, 2009 at 3:17 am
    Permalink

    more simple ;)

    $a = !empty($a);

  5. [...] 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 [...]

  6. Written by fqqdk on April 20, 2009 at 5:55 am
    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.

  7. [...] Excerpt from: PHP’s alternative syntax and why it’s useful | Ask About PHP [...]

  8. Written by Sylvain on April 20, 2009 at 9:04 pm
    Permalink

    You can also use short tags :

  9. Written by bloky on April 20, 2009 at 10:32 pm
    Permalink

    @#3,
    And you think problems writing a XML tag is good?

  10. Written by ken on April 21, 2009 at 4:47 am
    Permalink

    more simple ;)

    $a = !!$a;

  11. Written by Raymond Selda on April 21, 2009 at 9:50 pm
    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.

  12. Written by Jacob Rask on April 22, 2009 at 9:13 pm
    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.

  13. Written by perpa muhasebe on September 14, 2009 at 5:53 am
    Permalink

    PHP’s alternative syntax and why it’s useful | Ask About PHP great article thank you.

  14. Written by moto kurye on September 23, 2009 at 6:51 pm
    Permalink

    PHP’s alternative syntax and why it’s useful | Ask About PHP great article thank you.

  15. Written by syabac on July 11, 2010 at 5:33 pm
    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.

  16. Written by wildanr on July 15, 2010 at 7:12 pm
    Permalink

    So i guest we don’t need templating system anymore huh?

Subscribe to comments via RSS

Leave a Reply