CodingStandards » History » Version 62

« Previous - Version 62/64 (diff) - Next » - Current version
yllen, 06/03/2016 07:50 PM

validate on 09/07/09


To be more visible, don't put a block in comment with /*
but comment each line with //


3 spaces

Line width : 100

// base level
    // level 1
        // level 2
    // level 1
// base level

STATE: accepted : 3 spaces / 100 chars max

Control structures

Multiple conditions in several idented lines

if ($test1) {
     for ($i=0 ; $i<$end ; $i++) {
          echo "test ".( $i<10 ? "0$i" : $i )."<br>";

if ($a==$b
    || ($c==$d && $e==$f)) {
} else if {

switch ($test2) {
     case 1 :
          echo "Case 1";

     case 2 :
          echo "Case 2";
          // No break here : because...

     default :
          echo "Default Case";

STATE: accepted

Including files

include_once in order to include the file once and to raise warning if file does not exists


STATE: accepted

DONE: require_once , include, require -> include_once

PHP tags

Short tag not allowed. Use complet tags.


// code

STATE : accepted

TODO : nothing


Function names should be written in camelBack, for example:

function userName($a, $bldkjqmjk , $cldqkjmlqdsjkm, $ldqjlqdskj, $peaoizuoiauz, 
                  $lqdkjlqsdmj) {

Call static function

If the static function is
- in the class => self::
- in parent class => parent::
- in another class => ClassName::

STATE : accepted

TODO : check all functions


Class names should be written in CamelCase, for example:

class [[ExampleAuthentification]] {

STATE : accepted

TODO : check all classes


Variable names should be as descriptive and short as possible.

Normal variables should be written in lower case. In case of multiple words use the _ separator.

Global variables should be written in UPPER case. In case of multiple words use the _ separator.


$user         = 'glpi';
$users        = array('glpi', 'glpi2', 'glpi3'); // put elements in alphabetic order
$users        = array('glpi1'  => 'valeur1',
                      'nexglpi => array('down' => '1',                                        
                                        'up'   => array('firstfield'
                                                         => 'newvalue)),  // if too long for width of colomns 
                      'glpi2   => 'valeur2');
$users_groups = array('glpi', 'glpi2', 'glpi3');

$CFG_GLPI = array();

STATE : accepted

Variable types

Variable types for use in DocBlocks for Doxygen:

Type Description
mixed A variable with undefined (or multiple) type.
integer Integer type variable (whole number).
float Float type (point number).
boolean Logical type (true or false).
string String type (any value in "" or ' ').
array Array type.
object Object type.
ressource Resource type (returned by for example mysql_connect()).

Inserting comment in source code for doxygen.
Result : full doc for variables, functions, classes...

STATE : accepted

TODO : check all source code

quotes / double quotes

echo 'dqmkdqmsl';
echo 'toto'.$test.' est vivant';

After reading bench about strings :
- Best choice seems to be simple quote.
ex : echo 'toto' not echo "toto"
- best is to concat vars and string.
ex : echo 'toto'.$test.' est vivant.'
- Best choice between echo and print is echo.

- Best choice to construct string before make echo, result : decrease number of use echo.

Performance says to use simple quotes but it make using \n slower (using a constant)

Conclusion : Use double quotes (more lisible)

STATE : accepted

TODO : check all source code


Name in lower case.

Maximum line length : 100 characters

STATE : accepted

TODO : check all files


Capital letter :


STATE : accepted

TODO : check all constants


Queries must be written onto several lines, one MySQL item by line.

All MySQL words in UPPER case.

All item based must be slash protected (table name, field name, condition).

All values from variable, even integer should be single quoted

$query = "SELECT *
          FROM `glpi_computers`
          LEFT JOIN `xyzt` ON (`glpi_computers`.`fk_xyzt` = `xyzt`.`id`
                               AND `xyzt`.`toto` = 'jk')
          WHERE @id@ = '32'
                AND ( `glpi_computers`.`name` LIKE '%toto%'
                      OR `glpi_computers`.`name` LIKE '%tata%' )
          ORDER BY `glpi_computers`.`date_mod` ASC
          LIMIT 1";
$query = "INSERT INTO `glpi_alerts`
                (`itemtype`, `items_id`, `type`, `date`) // put field's names to avoid mistakes when names of fields change
          VALUE ('contract', '5', '2', NOW())";

STATE : accepted

TODO : check all source code


To optimize PHP7, it's better to not use string concatenation

echo "<input type='hidden' name='" . $name . "' value='" . $options['_projecttasks_id'] . "'>";

must be written
echo "<input type='hidden' name='$name' value='{$options['_projecttasks_id']}'>";