PHP Coding Standards and Naming Conventions

Basic Requirements

PHP 5 code is required.

All code must be E_STRICT-compatible. This means that it must not produce any warnings or errors when PHP's error reporting level is set to E_ALL | E_STRICT.

Layout and indentation

Indenting code is a must. Tabs size is 4 characters and your editor must insert TAB as TAB not as 4 SPACES

Array keys

Array keys myst be quoted with single quote (')

echo $row['something'];

Strings

Strings can be with single or double quote

$s = "This is 1st example"; $s = 'This is 1st example';

PHP variables in string

Must be in brackets, instead of this:

$s = "This is $variable example";

Use this

$s = "This is {$variable} example";

or

$s = 'This is '.$variable.' example';

Comments

Comment code ad much as possbile and as long as it makes sense.

/** * This is what function does * * @access public * @param $var1 (string) - username * @param $var2 (int) - user id * @return bool */ public function getUser ($var1, $var2 = 0) { // do something return true; }

Also use one line comments when needed

$activated = User::isValid(); // check if user activated account

No need to comment obvious things

while ($i < $something) { ... $i++; // increment $i by one <- THIS IS STUPID COMMENT }

Basic rules

Equal sign (=) must have at least one space before and one after

$a = 'something';

If you have more assignments one after another, try to align =

$a = 'something'; $another = 45;

Compare sign (==, =>, >, <, <=, !=) also need minimum one space before and after

if ($condition == 'I know')

If else

Always use brackets { } even for simple if () statement

if ($condition == CONSTANT) { $this->doSomethingCool ($condition); }

If you have more similar if/else statements, you can align brackets but you still have to use brackets:

if ($condition == CONSTANT) { $this->doSomethingCool ($condition); } elseif ($condition = 24) { Object::staticFunction (); } else { $this->doOtherThing (); }

Between "if", "for", "switch", "foreach" and () must be one space

Example
if ($condition == CONSTANT) { $this->doSomethingCool ($condition); } elseif ($condition = 24 || !empty($stuff)) { Object::staticFunction (); } elseif ($condition == 'I know') { Another_Object::newStaticFunction (); } else { $this->doOtherThing (); } public static function functionName ($variable_one, $variable_two = '') { if ($something) { $this->get(); } }
Multiple conditions

If you have if statements with long multiple conditions try to separate in different lines

if ($something == 'Something' && $another_thing > 45) { // do something }

For more complex conditions split in 2 lines

if (($something == 'Something' && $another_thing > 45) || (User::isValid() && $product->id)) { // do something }

Or even better

if ( ($something == 'Something' && $another_thing > 45) || (User::isValid() && $product->id) ) { // do something }

Conditions order

If you have multiple conditions put the most common condition first.

if (!empty($var) && $var == 'something')

So the second condition never checks if the first one is false ( $var is empty )

Switch

switch ($condition) { case 'something': // do something here break; case 48: // do the stuff // no break; - if you need case with no "break" you have to write comment case SOME_CONSTANT: default: // do some magic break; }

Functions

Function variables

All function variables myst be

function someFunction ($var1) { // function body } function otherFunction ($var1, $variable_two) { // function body } function thirdFunction ($var1, $variable_two = 'default') { // function body }

Functions with default variables as first parameters are just plain stupid

function thirdFunction ($var1 = 'default', $variable_two) { // function body }

Try to avoid more than 4-5 function parameters, but if you absolutely need it, then separate in several lines:

function thirdFunction ($var1, $another_variable, $third_variable, $yet_another_variable = 44, $variable_two = 'default') { // function body }

Loops

Array loops

For single dimension arrays user $val for array values and $key for array keys

foreach ($item as $key => $val) { // do something here }

Multidimensional array loops

For multi dimension arrays user $row for array values and $key for array keys

foreach ($data as $key => $row) { // do something here }

Nested loops

foreach ($data as $key => $row) { // do something here foreach ($row as $k => $v) { // do this for this row } }

For loops and counters

for ($i = 0; $i < 5; $i++) { // do something }

Nested For loops

for ($i = 0; $i < 5; $i++) { // do something for ($j = $i; $j < 100; $j++) { // do something } }

Operators

Allways use space between operators

$a=$something*$b+$i

Replace it with:

$a = $something * $b + $i;

Except for string concatenation

$s = 'This is my '.$order.' example';

Or

$s = 'And this is my '.($order + 1).' example';

Or

$s = 'And this is my '.round (max ($order / 100) * 100).'% example';

Exceptions

try { if (!App::dosomething()) { throw new Exception('Hey, we screwed up'); } // do whatever you wanted to do } catch (Exception $e) { customExceptionHandler ($e); }

Include() and require()

Include and require are statements not functions therefore use it like this:

include "/path/to/file/some.php"; require "/path/to/file/some.php";

Prefered methods are require_once and include_once

include_once "/path/to/file/some.php"; require_once "/path/to/file/some.php";

Appendix

Document Date and Version

Document created in december 2008. Version 1.0 beta