General Conventions:

  • Use tabs for all code indentation. Configure your editor to store tabs as tab characters, not spaces. Do not use a bunch of spaces for indentation.
  • New files in SVN / Git must use UTF-8 encoding (without BOM).
  • Unix-style line endings: LF
  • Filenames included in all directly accessible URLs must be lowercase; if a name consists of multiple words, connect them with underscores.
  • Line width: 120 characters

Code Standards

Examples

<?php // Must start with <?php
// xxx@example.com Each file must specify the owner's email

require_once('conf/config.inc.php'); // require_once must have parentheses, and there must be no space before the left parenthesis

class CodingStandard { // Add a space before braces. Class names start with an uppercase letter; multiple words each start with an uppercase letter
    private $attribute; // Property comments are written directly after the property

    // Array format
    public $color = array(
        '1' => 'red', // Indent once with a tab
        '2' => 'blue',
        '3' => 'yellow',
        '4' => array(
            '1' => 'green', // After aligning with the array columns above, indent once more with a tab
            '2' => 'gray'
        ) // The end of the array aligns with the very start of the declared variable
    ); // The end of the array aligns with where the array variable is declared

    public $number = array(1, 2, 3, 4); // For simple arrays, they can be placed on one line

    // Method comments use double slashes and should be completed on one line if possible
    function foo($i, $list) { // 1. There is no space before the ( after the function name 2. For multiple parameters, if there is a comma, there must be a space after it
        for ($j = 0; $j < $i; $j++) { // Add a space after for
            echo "This is no.{$j}, content is {$list[$j]}"; // echo statements do not use parentheses.

            // Whether to use single quotes or double quotes in echo statements depends on the actual situation
            echo '&lttable border="0" cellspacing="5" cellpadding="5"&gt';
        }
        if ($i > 0) { // 1. Add a space after if 2. Operators must have spaces on both sides
            return $i % 2; // Operators have spaces on both sides
        } else { // else must also have spaces on both sides
            return null;
        }

        if ($j == $i) return 1; // If an if contains only a single short statement, it is recommended to write it on one line; if splitting into multiple lines, it is recommended to add parentheses.

        $count = count($_SERVER); // Write the assignment outside
        if ($count > 10) echo 'pass'; // In if, only perform boolean judgment; do not write assignment statements
    }

    public static function testFunction() { // Static and non-static method naming both follow the camelCase principle
    }
}

$s = new CodingStandard(); // When creating an object with new, parentheses must be added
$s->foo(10, $s->color); // There must be no space inside the parentheses after a function. If a function has more than one parameter, there must be a space after each comma
CodingStandard::testFunction(); // Static code calls have only one way: the double colon call

// PHP files must end with ?>, and ensure there are no spaces or blank lines after it.
?>