.
A example function using the colon syntax is shown below:.
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:
Again I liked the article!