PHP Coding Standards

Posted on September 27, 2008

Coding standards is a very important thing in web development. Code must be formatted in such a way that everyone can read it. Although you start a project on your own and don’t imagine anyone else seeing the source, there may come a time where you sell the site and a new developer will need to work on it. Or you have to hire a developer to work on the site. It is best if you stick to one clear format. I personally follow the zend coding standard. In this post I am going to explain a bit about how to format your code in such a way as you follow the Zend Standards. This will make your code clean and easy to use for any developer reading your code and make it easy for yourself to read.

General Rules

Number 1: You don’t use “?>”. PHP does not require it. Do not end your files with ?> just leave it blank. It should not change anything at all in your code. It also prevents accidental whitespace injection into your files. The exception is when the file is both php and html. Although that is also bad practice. PHP shouldn’t really have HTML and PHP in the same file.

Number 2: Short tags are never allowed. Short tags are when you use instead of the full value which is .

Number 3: Indentation should be 4 spaces. Tabs aren’t allowed. Some IDE’s such as Dreamweaver allow you to set your tabs at 4 spaces. Number 4: You should aim to have a maximum of 80 characters per line. However Zend allows up to 120. This can also be set in your IDE. These should all be followed strictly.

Naming

The naming of files is important, files should be named appropriately and end in .php. There is no general rule but I like to have classes named as “ClassName.class.php”. It helps seperate classes from general php. It’s also easy for anyone looking at the code to find what they need. “MySQL.class.php” means they would know it’s a MySQL class. All characters in the name have to be alpha numberic. So either numbers or letters. Functions and methods are named starting with a lowercase then each new word starts with an uppercase. Underscores are not permitted. A function should be named along the lines of this. “phpIsAwesome( );”. Variable names can never contain underscores. Only alpha numberic characters. The same method as functions should be used when naming. This is the “$lowerUpper” variable naming. The one thing that does however need underscores are Constants. Constants should be underscored at each new word however all letters must be uppercase. “WE_LOVE_CONSTANTS”.

If and Else

If’s must have a single space before any conditions and a single space before an opening bracket. All code inside it should be properly indented with 4 spaces. This is one of the most vital rules as most people hate code without indentation. It is almost impossible to read. An example of good, clean and indented code is this:

if ( $a > $b ) {
	// Some Code
} else {
	// Some more code here
}

This is what you should aim for. Rather than a block of code with no indentation such as this:

if($a > $b){
//code and stuff
}else{
//other code
} 

Code like that is the pet hate of almost all developers.
Documentation
All documentation blocks must be documented using phpDocumentor format. This looks somewhat like this:

/**
 * Short description for file / class
 *
 * Long description for file / class (if any)...
 *
 * LICENSE: Some license information
 *
 * @copyright  2008 Your Company
 * @license    http://license.yoursite.com
 * @link       http://www.yourfile.com
 * @since      File available since Release 1.5.0
*/ 

Try and document alot of your code. It’s important so other developers know whats going on with your work. Remember that not everyone codes like you, everyone will have different ways of doing things. So explain what your doing but don’t go over the top.
The End
That’s all for this article on coding standards. I hope you learned somethings from this article and put them into practice. For a full list on zend coding standards read the Zend Coding Standards Manual. You do not have to adhere exactly to these, adding your own twists to the style.

Mark Cole.

Leave a Reply

You must be logged in to post a comment.

Sourcebits

iPhone Development and Flex Development

Categories