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.
Indenting code is a must. Tabs size is 4 characters and your editor must insert TAB as TAB not as 4 SPACES
Array keys myst be quoted with single quote (')
echo $row['something'];
Strings can be with single or double quote
$s = "This is 1st example";
$s = 'This is 1st example';
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';
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
}
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')
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
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();
}
}
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
}
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 ($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;
}
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
}
For single dimension arrays user $val for array values and $key for array keys
foreach ($item as $key => $val)
{
// do something here
}
For multi dimension arrays user $row for array values and $key for array keys
foreach ($data as $key => $row)
{
// do something here
}
foreach ($data as $key => $row)
{
// do something here
foreach ($row as $k => $v)
{
// do this for this row
}
}
for ($i = 0; $i < 5; $i++)
{
// do something
}
for ($i = 0; $i < 5; $i++)
{
// do something
for ($j = $i; $j < 100; $j++)
{
// do something
}
}
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';
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 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";
Document created in december 2008. Version 1.0 beta