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)
{
    $i=0;
 
    if(is_array($array)):
        $total_elements = count($array);
 
        while($total_elements > 1):
            if($array[$i] < $array[$i+1]):
                $i++;
                $total_elements--;
            else:
                return false;
            endif;
        endwhile;
 
        return true;
    endif;
}
 
$test_array = array(0,1,2,3,4,6,7,8,9,10);
echo is_array_ordered($test_array);

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

7 Responses

1

Jani Hartikainen

January 12th, 2009 at 4:07 am

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

$i=0;
if(!is_array($array)) {
return;
}

$total_elements = count($array);

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

$i++;
$total_elements–;
}

return true;

Less nesting is better =)

2

Martin Sarsini

January 12th, 2009 at 4:10 am

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

Jani Hartikainen

January 12th, 2009 at 6:19 am

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

Nick

January 15th, 2009 at 3:17 pm

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

Andrew Thompson

March 16th, 2009 at 1:30 pm

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

$a = $array;
sort($a);
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.

sameer

March 16th, 2009 at 10:18 pm

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

7

Chris London

April 20th, 2010 at 9:01 am

Great article. I’ve also wondered about the performance affects of colon syntax over braces. Anyone done benchmark testing?

There are other statements that can use the colon syntax. All of them are: if, for, foreach, switch, and while.

Source: http://www.kwista.com/programming/colon-syntax-in-php/

Again I liked the article!

Your thoughts

Sign up for fresh content in your email