Different Brace Styles in PHP
I’ve decided that I’ve been talking about coding styles for too long to not focus on any individual styles. So, let’s focus on a specific aspect of coding: brace styles.

Overview of brace styles
Many language constructs in PHP use braces, such as for,while, if, switch, etc. Where should the braces be placed? All of the different styles have different reasons behind them. I’m going to talk about all of the different styles individually and then assess the situation as a whole.
K&R style
This style was first introduced by Kernighan and Ritchie’s book The C Programming Language. It puts braces on the same line as the control statements. Here’s an example:
if( $conditionOne ) {
functionOne();
$variableOne = 1;
} else {
functionTwo();
while( $conditionTwo ) {
functionThree();
$variableTwo++;
}
}
functionFour();
The advantages of this style are that the braces line up with the control statements that “own” them.

It could be viewed that the actual “owner” of the closing brace is the opening brace, so it should line up with that. Another advantage is that code takes up less space since braces do not require lines on their own. A disadvantage is that it’s harder to scan for beginning and ending braces when using this style. I don’t really agree that that is a disadvantage though, I find it quite easy to find the opening brace from the closing brace. Since everything inside of the control statement is indented, you just have to go straight up from the closing brace to find the line with the opening brace. In my opinion, the control statement is much more important than the brace itself, so it’s better than when you follow the closing brace straight up you find the control statement.
Allman style
The Allman style is sometimes referred to as the “ANSI style”. It puts braces on lines of their own and indents them to be aligned with the control statement. Here’s an example:
if( $conditionOne )
{
functionOne();
$variableOne = 1;
}
else
{
functionTwo();
while( $conditionTwo )
{
functionThree();
$variableTwo++;
}
}
functionFour();
Instead of the brace lining up with only the control statement, it lines up with the opening brace too.

An advantage is that the control statement can be commented out to still have syntactically correct code. For example:
// if( $conditionOne )
{
functionOne();
$variableOne = 1;
}
A disadvantage is that each brace takes up a line of its own. This was a problem back when programmers were limited to working from terminal with small resolutions, but today it isn’t much a problem. Code readability is more important than code length.
Whitesmiths style
The Whitesmiths style is essentially the same as the Allman style, but braces are indented to be aligned with the body of code instead of the control statement. Here’s an example:
if( $conditionOne )
{
functionOne();
$variableOne = 1;
}
else
{
functionTwo();
while( $conditionTwo )
{
functionThree();
$variableTwo++;
}
}
functionFour();
I’m not going to highlight how things are aligned for this one. It’s pretty much the same as the previous except that things are indented a little differently. It has the same advantages and disadvantages.
GNU style
This style is the average of the previous two. The braces are indented to be in the middle of the control statement and the body of code. Here’s an example:
if( $conditionOne )
{
functionOne();
$variableOne = 1;
}
else
{
functionTwo();
while( $conditionTwo )
{
functionThree();
$variableTwo++;
}
}
functionFour();
Since a tab block is equal to four spaces, the braces are indented two spaces or one half of a tab block. This one has the same advantages and disadvantages as the last two. It’s a little easy to see the control statements and the different blocks of code in this one. Plus, all of the braces are indented so that they aren’t aligned with anything else, so it’s easy to see where the control blocks begin and end.
Horstmann style
The Horstmann style is similar to the Allman style. The only difference is that the first statement in the code body is placed on the same line of the opening brace. Here’s an example:
if( $conditionOne )
{ functionOne();
$variableOne = 1;
}
else
{ functionTwo();
while( $conditionTwo )
{ functionThree();
$variableTwo++;
}
}
functionFour();
An advantage of this is that it saves space. A disadvantage is that it’s harder to see where things start. Things are pretty much more cluttered. As I said earlier, readability is more important than the conservation of space.
Banner style
Banner style is pretty much K&R style except that the closing braces are also indented. Here’s what it would look like:
if( $conditionOne ) {
functionOne();
$variableOne = 1;
}
else {
functionTwo();
while( $conditionTwo ) {
functionThree();
$variableTwo++;
}
}
functionFour();
I don’t really think that there are any advantages to this style. Most people who use it do so because it makes it easier to see the control statements. The closing braces are indented because they aren’t as significant as the opening control statements.
Pico style
This is the final style. It’s used mostly with the Pico programming language. It’s similar to the Hortstmann style, but the closing brace is put on the same line as the last control statement. Here’s an example:
if( $conditionOne )
{ functionOne();
$variableOne = 1; }
else
{ functionTwo();
while( $conditionTwo )
{ functionThree();
$variableTwo++; } }
functionFour();
This is the most compressed style, but that doesn’t mean that it is the best. I find it very hard to read – it’s over compressed.
The best style
Which style do you prefer? That’s pretty much what it comes down to. There isn’t a single best style. All of the different styles have their advantages and disadvantages. Moreover, these are styles, not rules or regulations. Just as no one can dictate your clothing style, no one can dictate your coding style. You don’t have to use any of the styles listed here – they are just the most popular styles.
My favorite is the K&R style. I find it easiest to read. At this point, I’m so accustomed to using it that I don’t think about it.
What style do you use? Why do you use it? Please leave a comment, I look forward to hearing your thoughts!
Tags: Clean Code, Code Organization, Code Styles, PHP and Braces, Web Development










Comments:
WOW! I had the basic Idea how to use them, but I never knew that there are so many types too…
@Tech-Freak
Yeah, there are a lot of different styles. Thanks for the comment!
Whitesmiths style that I use, very ok :).
@therealbest.com
Thanks for your comment!
Trackbacks: