Posted by : Chris London in (Programming, Tutorial)
Colon Syntax in PHP
Tagged Under : colon syntax, perl, php syntax
Colon syntax is another way to style your PHP code. Instead of using curly braces you use colons. Below I have included an example of a couple different curly brace styles and also each statement using the colon syntax. I don’t know of any performance benefits or loss to using this syntax over any other. Please comment here if you have any more information about its affects on performance.
Common Coding Styles
My personal favorite:
for (condition) {
// code here
} elseif (condition) {
// code here
}
A very common coding style:
for (condition)
{
// code here
}
else if
{
// code here
}
My Schpeel About Indenting
Indenting is the absolute most crucial element in coding style. I don’t personally care where people put their braces as long as the indentations are correct. When you have no indentation or incorrect indentation it’s an utter nightmare trying to debug any issues.
Colon Syntax in PHP
PHP has a syntax that is very common in WordPress code but isn’t used much outside of it. This syntax is colon syntax. Basically you use a colon for opening braces and then an endstatement; for the closing brace. Here are examples for each of the different statements that support colon syntax:
if statement:
if (condition) :
// code here
elseif (condition) :
// code here
else :
// code here
endif;
switch statement:
switch (condition) :
case (condition) :
// code here
break;
case (condition) :
// code here
break;
default :
// code here
break;
endswitch;
for loop:
for (expression; expression; expression) :
// code here
endfor;
foreach loop:
foreach (expression) :
// code here
endforeach;
while loop:
while (condition) :
// code here
endwhile;
NOTE: do ... while loops are not support with the colon syntax!
I hope you have enjoyed this tutorial. Make sure and share this article with others if you liked it!

Gligh! It makes it look more like Perl. That’s what we call a Bad Thing ™.
I agree on indenting though. Always use tabs. NEVER use spaces. *shakes an angry fist at coding n00bs* Tabs allow a programmer to set the spacing to his preference without changing the code. Spaces force the look of however many spaces there are in any editor, and really screws up a lot of formatting. Any editor worth its salt should adjust the tabs you create without incident, but it’s much more difficult to handle old spaces you’ve typed, you communist.
I should note that “you” is not the fine people at Kwista >.>
Thanks for the comments. I TOTALLY agree with not using spaces. Some developer tools even replace the tabs you input with spaces. There are settings to turn this off which I recommend everyone turn off.
Not only do tabs allow other developers to set the spacing to their liking but they take up less disk space. Correct me if I’m wrong but I’m pretty sure 1 tab takes up 1 byte and 1 space takes up 1 byte. So if you do a 5-spaces tab instead of just a regular tab then your tabs will take up 5 times as many bytes per tab!
Yeah, but I’ll hand you 16 cents and cover all the hard drive space you’ll use on spaces if you were to count every line of code you could waste that space on over the next 10 years.
It IS less efficient for interpreters/compilers a bit. That DOES matter in things like PHP, albeit not in a noticeable manner.
That’s interesting. I didn’t know that tabs were less compiler efficient. It seems that that’s how us programmers like it. More human friendly vs compiler efficient, like using functions and OOP.
Woops. No, I meant 4 spaces are less compiler efficient than one tab. It was backing up your point after refuting the reason you gave for your point. >.>
Essentially, a compiler or interpreter is going to have to compare x number of spaces or y number of tabs. The less you have of either, the more efficient it can go through your code. It’s going through byte by byte, so it makes sense. This is the same reason you see all the spaces taken out and all the var/function names reduced to as few characters as possible in some javascript on sites like google. On comps it doesn’t amount to much. On things like OLD OLD OLD comps and cell phones, it can matter.