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.

A example function using the colon syntax is shown below:

function is_array_ordered($array)
        $total_elements = count($array);
        while($total_elements > 1):
            if($array[$i] < $array[$i+1]):
                return false;
        return true;
$test_array = array(0,1,2,3,4,6,7,8,9,10);
echo is_array_ordered($test_array);

7 thoughts on “The benefits of colon syntax for control structures

  1. I think rather than using different syntax, the above code could be written:

    if(!is_array($array)) {

    $total_elements = count($array);

    while($total_elements > 1) {
    if(!($array[$i] < $array[$i+1])) {
    return false;


    return true;

    Less nesting is better =)

  2. yes I like the idea of colon syntax structure, but what I like of my editor (jEdit) is that by selection an opening or closing curly braket and the editor pairs them together. I am sure with some modification it will work also with the colon syntax.
    So what I do is to use the traditional brakets and always comment the opening and closing, like
    } /* endif isset(var_name) */

  3. By commenting the closing brace you basically repeat yourself. So you have an if, and you decide to change how it works – you have to also update the comment, or suddenly it doesn’t make any sense at all.

    Don’t like.

  4. I agree, some times using the colon syntax is good, especially when mixing HTML and PHP. I think you should have shown an example that mixed the two though!

  5. Rather than using a different syntax or reducing the nesting, the above could be written:

    $a = $array;
    return $a == $array

    Less code is better =)

    Actually, I have no idea whether this’ll be slower or not, as sort is done in c.

  6. This was not an exercise in writing a sorting algorithm but just an example code to demonstrate the colon syntax.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>