PHP Coding Standards

From Web Developer Network Wiki
Jump to: navigation, search

Ok... now that we've got some more people working with PHP - here's some guidelines to make our code more consistent and readable.

Follow this code syntax formatting: Always use the formal PHP opening tag <?php and never the short <?

Function names are camel capped, lowercase first letter, then uppercase first letter of subsequent words.

Variables names are always lowercase using camel caps or underscores to separate words.

Constants are always UPPERCASE.

Functions have 'one-true-brace' - one brace on the line immediately following the function name.

function fooFunction()
{ // This is the 'one-true-brace'

Control structures (if, else, while, for etc) should always have braces with the opening brace inline with the control statement. EX:

if (true) {
    // blah
} else {
    // bleh

Control structures should have a space between the name and the opening parenthesis. Function calls should have parenthesis right next to the function name. EX:

if ($var1 == true) {

So spaces after if () no space after fooFunction()

function fooFunction($arg1, $arg2 = '')
    if (condition) {
        // statement
    } else {
        // statement
    return $val;


When you have a list of variables you are assigning, align all the equal signs.

$var1      = 'this';
$var2      = 'that';
$other_var = 'this and that';

Use single quotes (') instead of double quotes (") unless you specifically intend to... PHP allows you to use variable expansion within double quoted strings -

$var1 = 'this';
$var2 = "Variable expansion allows $var1";
// $var2 now contains the string 'Variable expansion allows this'

Because of this, only use double quotes when you have variables you want to expand within a string, in all other cases using single quotes is faster.

There are two primary language constructs for sending output, print and echo. Because they are not functions, don't use parenthesis, and remember that echo is faster than print. In PHP, always use echo unless you have a specific reason for using print.

echo $var1;
echo $var2;

print $var1; // Slower because the print language construct returns 1.

Class names are formal names and should begin with an uppercase letter, and use underscores to separate words. Always prefix anything for UNL with UNL_ Remember to use the 'one-true-brace' for classes. Ex:

class UNL_Services_Peoplefinder


When laying out your classes, it is useful to use one class per file, and separate classes into directories where underscores are present. For example, the class UNL_Services_Peoplefinder should be in the filesystem at UNL/Services/Peoplefinder.php

If you have any questions, follow the PEAR/Zend Framework coding standards.

Sample phpDocumentor Header

 * This library does some neat stuff.
 * PHP version 5
 * @category  Default 
 * @package   UNL_Blah
 * @author    Brett Bieber <>
 * @copyright 2007 Regents of the University of Nebraska
 * @license BSD License
 * @link
Personal tools