Use an indent of one tab per indent.
These include if, for, while, switch, etc. Here is an example if statement, since it is the most complicated of them:
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.
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.
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.
Function definitions follow the format of function calls, with the opening brace at the end of the line of the function declaration. The global variable list (if used) should be placed on the line immediately following the opening brace, with a blank line between the variable list and the first line of function code. If the global variable list is not neccessary, include a blank line between the opening brace and the first line of function code.
function fooFunction($arg1, $arg2 = '') { global $foo1, $foo2; 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 connect(&$dsn, $persistent = false) { if (is_array($dsn)) { $dsninfo = &$dsn; } else { $dsninfo = DB::parseDSN($dsn); } if (!$dsninfo || !$dsninfo['phptype']) { return $this->raiseError(); } return true; } |
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.
Anywhere you are unconditionally including a class file, use require_once(). Anywhere you are conditionally including a class file (for example, factory methods), use include_once(). Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with require_once() will not be included again by include_once().
Always use <?php ?> to delimit PHP code, not the <? ?> shorthand.
Constants should always be uppercase, with underscores to separate words.
File names should be all lowercase and contain no spaces. HTML files should have .html as the extension, and PHP files should have .html as the extension. Where possible, template files should match the name of the PHP file that will be using it, e. g., index.html would be the template file for index.html. Where one PHP file uses more than one template, the templates should be similarly named: user.html could use userlist.html and userform.html for a list of users and editing a user, respectively.