[PHP Code -> Brace Styles]

Brace Styles Reference

Picture of me and my dog

Many language constructs use braces for organizing code into related blocks. While braces are required to group related blocks of code together, the general placement of the braces is usually determined by the programmer.

Where should the braces be placed? All of the different styles below have different reasons.


K&R Style

This style was first introduced by Kernighan and Ritchie’s book The C Programming Language. This style 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();
			

Allman Style

The Allman style is sometimes referred to as the “ANSI style”. This style 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();
			

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();
			

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();
			

Banner Style

Banner style is pretty much K&R style except that the closing braces are also indented. Here’s what this style looks like:

if( $conditionOne ) {
    functionOne();
    $variableOne = 1;
    }
else {
    functionTwo();
    while( $conditionTwo ) {
        functionThree();
        $variableTwo++;
        }
    }
functionFour();
			

Pico Style

This is the final style. It’s used mostly with the Pico programming language. This style is 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();
			

The Best Style?

Which style do you prefer? There isn’t a single "best" style. All the styles have their advantages and disadvantages. Additionally, these are styles, not rules, nor regulations, just suggestions. Unless you work for an employer who has an established style, then 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 personal favorite style is the Whitesmiths style for ALL (PHP, JAVA, Javascipt, CSS, etc.) coding. I find it easiest to read and practice. In addition, this style forces only one statement (including braces) per line, which I consider to be "best" practice regardless of language. Also, using the same style for different languages makes all code easier to read -- at least for me.

You decide as what meets your needs and comfort. Happy coding!

tedd

Some of the above was taken from: Austin Gulati

You may use these script(s) as you need without obligation to me whatsoever. However, any Donation via PayPal would be greatly appreciated -- please note, this site does not accept advertising.