Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Ask About PHP:
PHP's alternative syntax and why it’s useful
Apr 16, 2009 @ 12:50:56

On 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 structures.

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!

The one specifically mentioned deals with "if" evaluations. There's two other options to the usual braces method - one being a shorthand using colons and a semicolon and the other using the ternary syntax.

tagged: alternative syntax control structure if example colon ternary

Link:

Sameer Borate's Blog:
The benefits of colon syntax for control structures
Jan 15, 2009 @ 14:48:33

In this recent post to his CodeDiesel.com blog, Sameer is promoting the use of colons in control structures over curly braces.

PHP offers a alternative syntax for some of its control structures- if, while, for, foreach, and switch, where you change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. [...] With the colon syntax you just have to match a ‘if’ with a ‘endif’ or a ‘for’ with a ‘endfor’. When you are mixing HTML with PHP in web pages the code can become quite dense and confusing if it uses a lot of braces.

An example is included showing its use in "if" and "white" statements. With most editors and IDEs supporting brace matching and code folding, this is less of an issue, but it can help make for cleaner code.

tagged: control structure colon curly brace alternative syntax advantage

Link:


Trending Topics: