PHP - Coding Standards

    In any programming language, it is important that we follow a particular standard or protocol. This allows different developers or coders to remain on the same page as their peer developers. It makes the code understandable and manageable. There are many reasons that influence developers to stick to these standards.

    • Following the same coding standard allows your team members to understand your code.
    • If you stick to protocols there are fewer chances to make mistakes.
    • Revising your code will not impact other developers.

    What protocols or standard should be kept in mind

    1. Line length and indentation

    Any line of code should have a maximum of 75-85 characters to increase the developer’s readability and uses 4 space for indentation.

    2. Control structures

    If you are using control statements within your program then having too many parentheses may confuse the starting and opening of the parameters parenthesis. It is always a good choice to use a space between the control keyword and the opening parenthesis for clear understanding of the function call. Example-

    if ((condition1) || (condition2)) {
    }elseif ((condition3) && (condition4)) {
    }else {
    default code;

    Switch statement

    switch (condition) {
    case 1:
    case 2:
    Default code;

    3. Function calls

    Unlike the control statements, there is no need to use space between the function name and the opening parenthesis. Also, no space should be used between the last parameter of the function and the closing parenthesis. Example-

    $num= func_call(par1, par2, par3);

    4. Function definition

    The most commonly used standard for defining the function definition is using BSD/Allman style. Example-

    function func_call($arg1, $arg2 = '') {
    if (condition) {
    return $var;


    PHP supports the use of two types of comments, single line and the multiple line comments. Example-

    if (condition) {
    /* code
    return $var;

    6. Variable names

    Variables should be in lower case separated with _. If you are dealing with global variables then you can prepend it with ‘g’. Use all caps global constants with _ as a separator. If you are using a static variable then you can prepend it with ‘s’. There are some other common points that can be kept in mind like using one statement per line, try to code small for any function, code blocks should be aligned well to find their start or end easily. People are also reading: