Simple Coding Style for PHP


Many programmers use different kinds of coding styles to write code I have my own version that (i think) is simple and easy-to-use. Before we proceed, please read the disclaimer below.

DISCLAIMER: I have made every attempt to present accurate information, but I cannot guarantee that inaccuracies will not occur. I (the author) will not be held responsible for any claim, loss, damage or inconvenience caused as a result of any information found on this website (blog).

Code Formatting and Layout:

Code Formatting and Layout – which includes indentation, line length, use of white space, and use of Structured Query Language (SQL).

Use Soft Tabs (soft tabs are not actually tabs each soft tab is represented by a certain number of regular spaces) instead of Hard Tabs (regular tab). The reason is that tab spacing is different for different editors. Tab width of five spaces is good.
Try to break up lines after every 80 characters because 80 characters is the standard width of a Unix terminal and is a reasonable width for printing to hard copy in a readable font.

Some examples:

if ($month == ‘september’ || $month == ‘april’ || $month == ‘june’ ||
$month == ‘november’) {
return 30;

This should be changed to following:

if ($month == ‘september’ ||
$month == ‘april’ ||
$month == ‘june’ ||
$month == ‘november’)
return 30;

This method works equally well for functions’ parameters:

“from: \r\n”);

SQL Guidelines:
We’ll follow the following rules:
  • Capitalize keywords
  • Break lines on keywords
  • Use table aliases to keep the code clean

Consider the following query:

$query = “select firstname, lastname from employees,
departments where employees.dept_id = department.dept_id
and = ‘Arts’”;

Above should be changed to following:

$query = “SELECT firstname, lastname
FROM employees e, departments d
WHERE e.dept_id = d.dept_id
AND = ‘Arts’”;

Using braces in control structures (e.g. if, for, foreach, while):
To avoid confusion, we’ll always use braces, even when only a single statement is being conditionally executed.

if( isset ($name))
echo “hello $name”;

Must be changed to following:

if( isset ($name))
echo “hello $name”;

Consistency in using braces:
We’ll use BSD style braces for conditional statements. In BSD style braces the braces are placed on the line following the conditional with the braces outdented to align with the keyword. Follow the same rule for functions.

if ($conditional)

Some more guidelines:
  • “foreach” is better than “for” which is better than “while”.
  • Never use nonsense names for variables e.g. foo, sz, var and etc.
  • Try not to use global variables but if for some reason use them then all global variables and constants will be represented by capital letters e.g. $CACHE_PATH, $FILE_NAME and etc.
  • Try making classes where possible.
  • Long-lived variable (not necessarily a global, or even in the main scope; it is simply a variable that is used through any significant length of code and/or whose representation can use clarification) should have concise but descriptive names.
  • Temporary variables should be short and concise. Numeric variables used for iteration should always be named i,j,k,l,m, and n.
  • We’re not going to use tags like the following . We’ll be using or <script language="”php”"> </script> (work in Front Page too).
  • Use echo not print.
  • We’ll be using C and C++ style comments i.e. “//” and “/* */”. We won’t use “#”.
  • Comments like the following are useless so avoid using them.
// Increment i

  • Using phpdoc for documentation.
  • For multiword names for variables, use underscores to break words.

$num_elements = count($elements);

  • Function Names: They should be all lowercase, and multiword names should be separated by underscores. e.g.

function print_hello($name)
echo “Hello $name”;

Class Names:
  • The first letter of a class name is capitalized. This visually distinguishes a class name from a member name.
  • Underscore should be used to simulate nested namespaces.
  • Multiword class names should be concatenated, and the first letter of each word should be capitalized. e.g.

class XML_RSS {}
class Text_PrettyPrinter {}

Method Names:
The Java style is to concatenate words in multiword method names and uppercase the first letter of every word after the first. e.g.

class XML_RSS
function startHandler() {}

Naming Consistency:
Variables that are used for similar purposes should have similar names. e.g.


Matching Variable Names to Schema Names:
Variable names that are associated with database records should always have matching names.

$query = “SELECT firstname, lastname, employee_id
FROM employees”;
$results = mysql_query($query);
while(list($firstname, $lastname, $employee_id) = mysql_fetch_row())

We’ll use phpDocumentor to generate documentation on the fly. Please take a look at the software


Post a Comment

Subscribe to Post Comments [Atom]

<< Home