PHP | Coding Standards

PHP follows few rules and maintains its style of coding. As there are many coders and developers all over the world, so each of them can follow different coding styles and standards but this would have raised great confusion and difficulty for a developer to understand another developers code. It would have been hard to manage and store the code for future reference. Here is where the coding standards come into play. This not only makes a code easy to read but also makes the code very easy to refer in the future. This makes the code understandable and clearer to decipher, just like a blueprint. This also makes the code more formal and industry or software oriented. Below mentioned are few guidelines that one must follow in order to maintain the standard of PHP coding.

  1. PHP tags : One must use the PHP standard tags(), rather than the shorthand tags() to delimit the PHP code.
  2. Commenting : Use of standard C and C++ commenting style i.e., (//) – for single line and (/* */) – for multi-line, is highly encouraged and use of Python or Perl style of commenting i.e., (#), is discouraged.
  3. Line length and Indentation : It is a standard recommendation to not exceed more than 75-85 characters per line of code. One must not use tabs for indentation instead use 4 spaces as it is the standard indenting method in most of the programming languages.
  4. Structuring the control flow statements: The control flow or conditional statements must be written in such a way so that it could be differentiated from function call statements. While writing if, for, while, switch and other control flow statements there must be one space between the keyword and the opening parenthesis.

    $n = 5;
    if ($n > 0){
        echo "Positive";
    elseif ($n < 0){
        echo "Negative";
        echo "Zero";


  5. Function Calls : While writing a function call statement, there must be no space between the function name and the opening paranthesis. Example:
    echo testFunc(5, 6);
    function testFunc($num1, $num2) {
       $val = $num1 + $num2;
       return $val;


  6. Naming Variables : Here are few conventions that one must follow in order to name the variables:
    • Use of lower case letters to name the variables.
    • Use of ‘_’ to separate the words in a variable.
    • Static variable names may be started with a letter ‘s’.
    • Global variable names must start a with letter ‘g’.
    • Use of upper-case letters to define global constants with ‘_’ as a separator.
  7. Block allignment : Every block of code and curly braces must be alligned.
  8. Short Functions : All functions and methods must limit themselves to a single page and must not be lengthy.

Note: The above rules are provided just to make the code easily understandable and to manage the code easily. There will not be any specific error in case of violation of the rules mentioned above. But it is highly recommended to follow the above convention to improve the overall quality of code from a developer’s perspective.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

leave a comment



load comments

Subscribe to Our Newsletter