Coding Conventions (PHP)

From Wiki
Jump to navigation Jump to search

Sublime Text Snippets

Modified Sublime Text snippets to follow the coding style: File:Snippets-PHP.7z

Copy them to ~/Library/Application Support/Sublime Text 3/Packages/PHP and you are done.

Indenting and Whitespace

Use an indent of 1 Tab ↹, no spaces.

Lines should have no trailing whitespace at the end.

Files should be formatted with \n as the line ending (Unix line endings), not \r\n (Windows line endings).

All text files should end in a single newline (\n). This avoids the verbose "\ No newline at end of file" patch warning and makes patches easier to read since it's clearer what is being changed when lines are added to the end of a file.


All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability. For example, an assignment should be formatted as $foo = $bar; rather than $foo=$bar;. Unary operators (operators that operate on only one value), such as ++, should not have a space between the operator and the variable or number they are operating on.


Put a space between the (type) and the $variable in a cast: (int) $mynumber.

Naming convention for variables and functions

  • preprocessor macros/constants: name in upper case
  • typedefs: name usually all upper case
  • constants: upper case
  • globals: first letter lower case g_
  • session: first letter lower case s_
  • cookie: first letter lower case c_
  • locals: camel case or separated by _ e.g.: longVar or long_var
  • functions: camel case (first letter lower case)

Code layout

If you remember nothing else, remember this: thou shalt not use K&R style braces. K&R style braces is code that has the open curly brace on the same line as the preceding C control structure.

if (condition1 || condition2) {        // bad style
    action1;                           // bad style
}                                      // bad style
if (condition1 || condition2)          // good style 
{                                      // good style
    action1;                           // good style
}                                      // good style

Control Structures

Control structures include if, for, while, switch, etc. Here is a sample if statement, since it is the most complicated of them:

if (condition1 || condition2) 
elseif (condition3 && condition4) 

Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.

Always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

For switch statements:

switch (condition) 
    case 1:

    case 2:


For do-while statements:

} while ($condition);

Function Calls

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:

$var = foo($bar, $baz, $quux);

As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:

$short        = foo($bar);
$longVariable = foo($baz);