Indenting and Line Length
It is recommended to keep lines at approximately 75-85 characters long for better code readability.
Control Structures
<?php
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
defaultaction;
}
?>
Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
defaultaction;
}
?>
You are strongly encouraged to 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:
<?php
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
?>
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
?>
Split long if statements onto several lines
Long if statements may be split onto several lines when the character/line limit would be exceeded. The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators (&&, ||, etc.) should be at the beginning of the line to make it easier to comment (and exclude) the condition. The closing parenthesis and opening brace get their own line at the end of the conditions.Keeping the operators at the beginning of the line has two advantages: It is trivial to comment out a particular line during development while keeping syntactically correct code (except of course the first line). Further is the logic kept at the front where it's not forgotten. Scanning such conditions is very easy since they are aligned below each other.
<?php
if (($condition1
|| $condition2)
&& $condition3
&& $condition4
) {
//code here
}
?>
The first condition may be aligned to the others.if (($condition1
|| $condition2)
&& $condition3
&& $condition4
) {
//code here
}
?>
<?php
if ( $condition1
|| $condition2
|| $condition3
) {
//code here
}
?>
The best case is of course when the line does not need to be split. When the if clause is really long enough to be split, it might be better to simplify it. In such cases, you could express conditions as variables an compare them in the if() condition. This has the benefit of "naming" and splitting the condition sets into smaller, better understandable chunks:if ( $condition1
|| $condition2
|| $condition3
) {
//code here
}
?>
<?php
$is_foo = ($condition1 || $condition2);
$is_bar = ($condition3 && $condtion4);
if ($is_foo && $is_bar) {
// ....
}
?>
$is_foo = ($condition1 || $condition2);
$is_bar = ($condition3 && $condtion4);
if ($is_foo && $is_bar) {
// ....
}
?>
Function Calls
<?php
$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: $var = foo($bar, $baz, $quux);
?>
<?php
$short = foo($bar);
$long_variable = foo($baz);
?>
To support readability, parameters in subsequent calls to the same function/method may be aligned by parameter name: $short = foo($bar);
$long_variable = foo($baz);
?>
<?php
$this->callSomeFunction('param1', 'second', true);
$this->callSomeFunction('parameter2', 'third', false);
$this->callSomeFunction('3', 'verrrrrrylong', true);
?>
$this->callSomeFunction('param1', 'second', true);
$this->callSomeFunction('parameter2', 'third', false);
$this->callSomeFunction('3', 'verrrrrrylong', true);
?>
Class Definitions
<?php
class Foo_Bar
{
//... code goes here
}
?>
class Foo_Bar
{
//... code goes here
}
?>
Function Definitions
<?php
function fooFunction($arg1, $arg2 = '')
{
if (condition) {
statement;
}
return $val;
}
?>
Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate. Here is a slightly longer example: function fooFunction($arg1, $arg2 = '')
{
if (condition) {
statement;
}
return $val;
}
?>
<?php
function connect(&$dsn, $persistent = false)
{
if (is_array($dsn)) {
$dsninfo = &$dsn;
} else {
$dsninfo = DB::parseDSN($dsn);
}
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}
return true;
}
?>
function connect(&$dsn, $persistent = false)
{
if (is_array($dsn)) {
$dsninfo = &$dsn;
} else {
$dsninfo = DB::parseDSN($dsn);
}
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}
return true;
}
?>
Arrays
<?php
$some_array = array(
'foo' => 'bar',
'spam' => 'ham',
);
?>
$some_array = array(
'foo' => 'bar',
'spam' => 'ham',
);
?>
Comments
Non-documentation comments are strongly encouraged. A general rule of thumb is that if you look at a section of code and think "Wow, I don't want to try and describe that", you need to comment it before you forget how it works.
C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is discouraged.
Naming Conventions
Classes
Log | Net_Finger | HTML_Upload_Error |
Class Variables and Methods
$counter | connect() | getData() | buildSomeWidget() |
$_status | _sort() | _initTree() |
The following applies to PHP5.Protected class members are not preceded by a single underscore. For example:
protected $somevar | protected function initTree() |
Constants
DB_DATASOURCENAME | SERVICES_AMAZON_S3_LICENSEKEY |