{curly brackets} versus :colons/semi-colons; in PHP

Current and former colleagues of mine, will attest to my intense hatred of poorly written code and by poorly written code I mean code that is hard to follow.

In a bid to make my PHP code even more readable I’m dumping the curly brackets traditionally used with control structures (e.g. ifs, fors, switches) in favour of the colons and semi-colons of PHP’s alternative alternative syntax for control structures.

PHP’s traditional syntax for control structures – with curly brackets {}

  1. <?php
  3. // If statement
  4. if(1==1){
  5. doSomething();
  6. }
  7. else if(1>1){
  8. doSomethingElse();
  9. }
  10. else{
  11. doSomethingSpecial();
  12. }
  14. // For loop
  15. for($i = 1; $i < 5; $i++){
  16. echo $i;
  17. }
  19. ?>

Upon quick glance the usage of curly brackets it is obvious which bracket belongs to which control but it soon becomes more difficult to read when functions/methods are thrown into the mix and lots of nested controls exist.

Too many curly brackets {{{{{{}}}}}}

  1. <?php
  3. // Nested if's and foreach's statement inside a function
  5. function myFunction(){
  6. if($trees == 'green'){
  7. if($trees > $children){
  8. foreach($array as $key => $value){
  9. if($trees != 'dead'){
  10. doSomething();
  11. }
  12. }
  13. }
  14. else{
  15. doSomethingElse();
  16. }
  17. }
  18. else{
  19. doSomethingSpecial();
  20. }
  21. }
  23. ?>

The example above, shows it becoming a little more difficult to follow which bracket refer to which control. The very fact that my method of displaying code samples (codeviewer) removes indentation/tabs from the code makes this even ore evident.

PHP ‘s alternative alternative syntax for control structures :;

  1. <?php
  3. // If statement
  4. if(1==1):
  5. doSomething();
  6. elseif(1 > 1):
  7. doSomethingElse();
  8. else:
  9. doSomethingSpecial();
  10. endif;
  12. // for loop
  13. for($i = 1; $i < 5; $i++):
  14. echo $i;
  15. endfor;
  17. // nested if statements inside a function
  18. function myFunction(){
  19. if($trees == 'green'):
  20. if($trees > $children):
  21. doSomething();
  22. else:
  23. doSomethingElse();
  24. endif;
  25. else:
  26. doSomethingSpecial();
  27. endif;
  28. }
  30. ?>

For me, I find this so much easier to read. Replacing the last curly bracket (}) is a statement of the following endif;, endfor;, endswitch; etc. This means there less confusion as to what things are referring to.


It is imperative to write code that makes sense for other developers in your team or for the developers will inherit your code.

I find this alternative control structure much easier to read but what do you think? Do you find it easier or harder to read?

11 responses to “{curly brackets} versus :colons/semi-colons; in PHP”

  1. @stuart: yes it has but I’ve just gone in and updated the CSS, so it should be indented properly if you F5 the site.

  2. Curly braces are not especially hard to read if you have experience in any C-style language (such as PHP). Most editors will show you a matching brace (but will not show you a matching endfor/endif), so it depends on the experience and viewpoints of your fellow devs and which tools you have in place to support development.

    I do agree that the alternate syntax is slightly easier to track when using PHP embedded in large HTML files ( (we use them here for views/templates only), but I’d be a little wary of forcing other developers (that you may never talk to) to maintain such an uncommon coding style for classes and other files. Any personal style which could be referenced after your employment with the company as ‘Phil had a habit of…’ is something I’d think long and hard before enforcing adoption.

    Plus you’re still tied into using curly braces for your classes, methods, functions, third party code, etc, so I’d consider it muddying the syntax waters.

  3. @Mark: I see your point. If anything that the fact that the curly brackets are also used for classes, methods et al means it can get even more confusing.

    I found myself, commenting all my curly brackets like so: } // end if or } // end for loop just to be able to remember what they belonged to, so altering the syntax feels like natural progress to me.

  4. I share your hatred of poorly written code. However, I agree with Marks comment (excellent comment by the way ;)). The alternative syntax is very useful for your views/templates but for other stuff I would be tempted to keep the curly braces. One of the main reasons is bracket matching. I know its a small feature of code editors, but what a helpful little feature it is.

  5. You still run into the same problem if you start nesting lots of functions using this alternative syntax. Maybe not as soon as with braces, but not much later. I do like these syntaxes (correct plural?) but I prefer to use comments to label my closing braces, just like how many people label closing tags in html. Usually a comment like ” } //if (condition) ” is enough to jog my memory and help me figure out which opening brace a particular closing one corresponds to.

    Like I said though, I do like the ability to use endif, reminds me of bash :)

  6. @Ian: which editors do you use which do support bracket matching and not the alternative syntax matching. As I only use a combo of either DreamWeaver/Coda which (correct me if I’m wrong) don’t support either syntax but I’ve just downloaded Textmate in the last 5 minutes and that seems to support both.

  7. Coda definitely has brace matching (it ‘pings’ the matching brace when you move your caret over it). Doesn’t look like Dreamweaver does, Textmate does also (changes background colour on braces when caret’ing across the matching brace, but not on alt. syntax). Doesnt look like any of them can highlight the endx; syntax though.

    Forgot to clarify earlier that the reason I use alternative syntax in views/templates is that, when I have a large number of documents open, it makes it very clear which ones are ‘templates’ and which ones should actually do the work (acts as a quick visual hook per file to the context of development).

  8. @Mark James: I see, I’ve just checked in Coda and it does indeed do what you say. Double-clicking the bracket actually highlights the entire block. Which is very useful. Textmate will collapse an entire block if you press F1 with both syntax styles.

  9. I use Textpad (windows only – but is very good) ctrl-m on a bracket will match it up. I often use smarty for templates but if only using php then the short hand syntax comes in very useful.