A good amount of this is "borrowed" from the WordPress style guide as a fair amount of Real Group development is working on WordPress code and plugins.
There are differences though, and also additional items not documented there.
Any code indentation must be indented to four spaces. Configure your IDE to replace tabs with spaces to ensure that indentation remains the same in any editor.
Each item in an associative array should start on a new, indented line when the array contains more than one item:
$array = (
'foo' => 'bar',
'hello' => 'world',
);
Use single and double quotes when appropriate. If you’re not evaluating anything in the string, use single quotes. You should almost never have to escape quotes in a string, because you can just alternate your quoting style, like so:
echo '<a href="/static/link" title="Yeah yeah!">Link name</a>';
echo "<a href='$link' title='$linktitle'>$linkname</a>";
Final items in arrays should include a comma. This makes it easer to change the order of the array and provides cleaner diffs in version control.
$my_array = array(
'foo' => 'somevalue',
'foo2' => 'somevalue2',
'foo3' => 'somevalue3',
'foo34' => 'somevalue3',
);
Braces must be used for all code blocks in this style:
if ( condition ) {
action1();
action2();
} elseif ( condition2 && condition3 ) {
action3();
action4();
} else {
defaultaction();
}
Braces should use the block style, even for single conditions or when they are not required. As such, single-statement inline control structures must not be used.
if|else conditionals should favour elseif for consistency with label blocks.
When adding multi-line PHP snippets in HTML, PHP open and close tags should always be on a single line alone.
Shorthand PHP tags must never be used.
Class names should use capitalized words with no spaces or underscores. Any acronyms should be all uppe case.
class MyNewClass extends MyClass { [...] }
Methods within classes should use a camelCasee format. The first letter should be lowercase and subsequent words should start with and uppercase character.
public function myFunction() { [...] }
Constants should be in all upper-case with underscores separating words:
define( 'MY_CONSTANT', true );
File names should match the name and case structure of the class within. Eg. MyNewClass should have the filename MyNewClass.php.
Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.
x == 23
foo && bar
! foo
array( 1, 2, 3 )
$baz . '-5'
$term .= 'X'
Put spaces on both sides of the opening and closing parentheses of if, elseif, foreach, for, and switch blocks.