PHP coding standards for Web development

Rules will help you do better and perfect, in programming language also has some rules to show your code structure and more responsive. The coding standard help you make good developer. In this tutorial i will let you best coding practice rules in php which keeps you mind when you are developing application.

PHP Coding Standards

The Zend Framework is the de facto standard for PHP frameworks. We should follow the Zend Framework coding standards as closely as possible:

1- Brackets

Opening brackets for classes, functions, and methods must be placed on the line after the opening statement.

class MyClass  
    protected $_value;  

2- Inline IF statements

Never use an inline if statement. Always use block if statements:

if (true) {  
    return ‘it is true’;  

3- Tabs

Use 4 spaces for indentation. Do not use tabs.

4- Docblocks

Every method MUST have a docblock that contains a description of the method, the parameters the method accepts, a description of each parameter, and the return type of the method with an optional description about the return value.

 * This is an example 
 * {{one space goes here}} 
 * @param string $value Value to search 
 * @param bool $isTrue (optional) Set to TRUE for testing 
 * @param int $total (optional) Total number of records to retrieve 
 * {{one space between params and return annotation}} 
 * @return array 
public function example($value, $isTrue = false, $total = 10)  

5- Returning a value

The return line of a method or function should in most cases have an empty new line before the return statement so that it is easier to visualize. If the method body is a one-liner containing a return statement, a new line space is not needed.

Here are some examples of how the return statement(s) of a method should look:

public function blah()  
    return true;  
public function foo()  
    $a = ’123′;  
    // more calculations on $a  
    return $a;  
public function bar()  
    $a = ’123′;  
    if ($a == ’123′) {  
        return ‘baz’;  
    } else {  
        return ‘baz bar’;  

6- Class file formatting

Class files must contain the opening php tag statement on the first line. The following line must either be a docblock describing the file, or a new line. Describing the CLASS on the line directly after the opening php tag statement is wrong.

After the newline or file docblock, specify the class namespace (if applicable). If you are using (importing) other namespaces or classes into the class file, skip a line and start the use statement, specifying individual classes using commas and indented new lines (see example).

All classes should have a class level docblock that describes what the class does, has a @category, @package, and @subpackage docblock annotations. We do not include @author tags in our classes as we will all most likely touch every class over time. The class level docblock should always have one newline preceding it and no new lines after it.

The last line of a class file should be the closing bracket on its own line, left aligned, with no spaces before or after it.

Here is an example of a class the way we should format them. Do not stray from this example in terms of indentation, new lines, or style.

            ‘email’ => array(  
                ‘operator’ => ‘is’,  
                ‘searchValue’ => $email  
        return $response;  

7- Commenting in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.

// is a single-line comment
# This is also a single-line comment

This is a multiple-lines comment block
that spans over multiple

8- Naming Convention for Variables

Variable names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged in most cases.


For instance variables that are declared with the “private” or “protected” modifier, the first character of the variable name must be a single underscore.

private $_fi;

9- Naming Convention for Methods and Functions

Function names may only contain alphanumeric characters.Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized.


For methods on objects that are declared with the “private” or “protected” modifier, the first character of the method name must be an underscore.

protected _findElementId()