Skip to content

Instantly share code, notes, and snippets.

@boo1ean
Created October 17, 2012 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boo1ean/3905811 to your computer and use it in GitHub Desktop.
Save boo1ean/3905811 to your computer and use it in GitHub Desktop.
Code conventions

General

PHP code must always be delimited by the full-form, standard PHP tags and be terminated with semicolon:

<?php
    $model = $web->getModel();
    $title = $model->getTitle();
?>

For single-line statements:

<?php echo $title; ?>

Indentation

Indentation should consist of 4 spaces. Tabs are not allowed.

Maximum Line Length

The target line length is 80 characters. However, longer lines are acceptable in some circumstances.

String Concatenation

Strings must be concatenated using the "." operator. A space must always be added before and after the "." operator to improve readability:

<?php 
$character = 'Spider' . ' ' . 'Man';

Commas

Put a space after commas in single-line statements:

<?php
$this->someMethod($first, $second, $third);

Associative Arrays

When declaring associative arrays with the Array construct, breaking the statement into multiple lines is encouraged. In this case, each successive line must be padded with white space such that both the keys and the values are aligned:

<?php 
$sampleArray = array(
    'firstKey'  => 'firstValue',
    'secondKey' => 'secondValue',
);

Control Statements

Within the conditional statements between the parentheses, operators must be separated by spaces for readability. The opening brace is written on the same line as the conditional statement. The closing brace is always written on its own line. Any content within the braces must be indented using four spaces.

<?php
if ($value != 2) {
    $success = true;
} else {
    $success = false;
}

Of course it could be replaced with:

<?php 
$success = $value != 2;

But now I want to describe only code conventions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment