The benefits of colon syntax for control structures

Most PHP programmes may not be familiar with the colon syntax used for control structures. If you regularly code in wordpress than you already are acquainted with it.

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. The main advantage I’ve found in using this syntax is that it makes deeply nested code intelligible to read. If you have a deeply nested code it sometime becomes hard to pair the starting and ending brace. 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. At that time the colon syntax can come quite handy.

Read More