By heavyworks
via heavyworks.net
Published: Jan 04 2010 / 04:30
While it’s not a great secret, many people still not aware of the alternative syntax for control structures under PHP.
The C-like syntax for control structures are very intuitive but can deal lots of trouble when your code has lot of pages and lots of ifs, fors, while and therefore lots of opening and closing braces.
This article discuss the use of this alternative syntax that is great for improving your code readability.



Comments
jakyra replied ago:
If you've got deeply nested if statements where {} are a problem maybe you should consider refactoring your code...
heavyworks replied ago:
Sure jakyra, I strongly agree with you. No function should have more than a page of code... or half!
antych replied ago:
This syntax is very useful (and usually recommended) for php mixed with html in templates, view scripts etc.
Your examples are pretty useless, they don't show any advantages of using it.
heavyworks replied ago:
Antych, the examples are just illustrating the syntax, the explanation of the advantages were kept in the text only. Do you have a good case that we can use to exemplify the advantage?
antych replied ago:
A good use case could be a php code embedded in html for printing tables rows. In that case it's much easier to spot where the statement ends if you have > instead of >. This is especially useful if combined with other statements.
Normally this is not an issue because plain php code can be kept small and properly indented, but this is not the case when dealing with content (html). html is the key here, this is where this syntax becomes useful.
You can find some examples in ZF documentation, like top of this page http://framework.zend.com/manual/en/zend.view.scripts.html
Voters For This Link (9)
Voters Against This Link (2)