Skip to content
zenmiu edited this page Aug 4, 2011 · 3 revisions

Opening and closing PHP tag

Required:

  1. For files containing PHP code only, the closing tag ("?>") is not entered [REQ.PHP.2.1.1].
  2. The opening tag must be complete [REQ.PHP.2.1.2].

Maximum line length

Required:

Line length cannot exceed 120 characters, except: [REQ.PHP.2.2.1].

  1. Declarations of class properties.
  2. Individual test lines.
  3. Test blocks (so-called heredoc and nowdoc).
  4. In class declarations if the implements block is placed on a new line and still exceeds the length restriction.

Examples of exclusions:

class A {
    protected $z = ‘111....150 symbols...111’;
}

$a = ‘111....150 symbols...111’;

func(
    ‘111....150 symbols...111’
);

$a = array(
    ‘111....150 symbols...111’,
);

$a = <<<DOC
111....150 symbols...111
DOC;

New lines

Required:

  1. New line character - line feed character (LF or \n or 10 or 0x0a) [REQ.PHP.2.3.1].
  2. In all the cases when the text with the new line is not expected to go beyond the current server, use the PHP_EOL constant for the line feed character.
  3. In the cases when the text with the new line is expected to be transferred to other computer, use the Windows-based line feed character (\n\r or 10+13 or 0x0a + 0x0d).

##Indents/tabs/spaces

Required:

Indent for each new level - 4 spaces [REQ.PHP.2.4.1].

Recommendations:

Place as many indents as necessary but not more than you actually need. If you are indenting further than the 7th level, think about taking the code into a separate block [WRN.PHP.2.4.1].

Justification:

  • When people use different tab values, the code often appears unreadable and unprintable; therefore, spaces are more preferrable.
  • Nobody will ever come to an agreement on the most suitable number of spaces. Just be consistent. 4 spaces are the most common.
  • The majority of PHP applications use 4 spaces.
  • The majority of text editors use 4 spaces.

Example:

function func() {
    if (something bad) {
        if (another thing bad) {
            while (more input) {
            }
        }
    }
}

Control structures

Required:

  1. In control structures, there must be a space between the key word and the opening bracket [REQ.PHP.2.5.1].
  2. The opening bracket is to be placed on the same line with the respective operator [REQ.PHP.2.5.2].
  3. The closing bracket is to be placed on the same indent with the operator [REQ.PHP.2.5.3].
  4. The closing curly bracket is to be placed on the next (following the conventional code) line [REQ.PHP.2.5.4].
  5. The entire content between the brackets is to be indented [REQ.PHP.2.5.5].
  6. The code in the body is always framed by curly brackets [REQ.PHP.2.5.6].
  7. Values cannot be assigned to variables within control structures, except within for [REQ.PHP.2.5.7].
  8. If a code block within curly brackets is large (a body is large when it has 4 or more lines of code or 1 line of comments to the body), it must be preceded by a blank line [REQ.PHP.2.5.10] [!].

Extension for operator if/else/elseif

Required:

  1. The elseif operator is placed on a single line [REQ.PHP.2.5.8].
  2. A block of code within curly brackets is to be followed by a blank line if it is followed by else or elseif [REQ.PHP.2.5.9] [!].
  3. Use elseif instead of the else if combination [REQ.PHP.2.5.11].

Example:

if ((condition1) || (condition2)) {
    action1;
} elseif ((condition3) && (condition4)) {
    bigaction2;
} else {
    defaultbigaction;
}

Extension for operator Switch

Required:

  1. The case block, passing the control further down, must have a comment [REQ.PHP.2.5.12].
  2. The default block is always present [REQ.PHP.2.5.13].
  3. Each case block must be followed by a blank line, separating the block from a neighbor [REQ.PHP.2.5.14].
  4. The content of each case expression must be indented [REQ.PHP.2.5.15].
  5. One blank line must be placed between switch, before the first case expression.

Example:

switch (...) {
    case 1:
        // break intentionally omitted
    case 2: {
        $v = get_week_number();
        ...
        }
        break;
    default:
        ...
}

Extension for ternary conditional operator ? :

Recommended:

  1. Enclose the condition expression in brackets, thus distinguishing it from the rest of the code [WRN.PHP.2.5.1].
  2. Where possible, the actions to be performed on the condition should be simple functions.
  3. If the entire branch block, located on a single line, is difficult to read, place the variant1 and variant2 blocks on a separate line each.

Example:

(condition) ? funct1() : func2();

or

(condition)
    ? long statement
    : another long statement;

Extension for operators for, do .. while and while

Recommended:

Minimize the use of continue and break [WRN.PHP.2.5.4].

Clone this wiki locally