The document provides PHP coding standards and conventions for naming variables, functions, classes, and files. It also includes guidelines for formatting SQL, code in general, braces and parentheses, comparisons, strings, comments, and PHPDoc.
Naming Variables Useall lower case. Use _ to separate words.eg.$green_color_value Use descriptive names (except loop variables). Loop variables can be of the usual variety: $i, $j, $k, etc. Count variables should follow the format $*_count. eg $bug_count
3.
Naming Functions Use camel case. e.g. thisFunction() or setupPageBreaks()
4.
Naming Classes Use FirstLetterOfWordIsCaptilized style, eg. AccountingCashbook
5.
Naming Files Use all lower case. Use _ to separate words. eg. view_new_bugs_page.php Maximum number of _ to be used is 3. In unavoidable cases MAX could be 4. Filenames must be less than 32 characters in length. This plays nice with older file systems like MacOS. Included files should be suffixed by .inc.php
6.
SQL Formatting UPPERCASE all SQL keywords: $query = "SELECT * FROM item WHERE id=‘$item_id’";
7.
General Formatting Use TABS with a size of 4 Use <?php ?> for php delimiters.
8.
Braces and Parenthesis Paranthesis should be right after a function name. eg: function() not function () Paranthesis should have a space right after a keyword (if, while, for) eg: for (...)
9.
Braces Formatting Arraysshould be referenced with no spaces. eg: $arr['index'] not $arr[ 'index' ] for (...) { Statement Block }
Braces Formatting if... else blocks should be in this format: if (...) { Statement Block } else { Statement Block }
12.
Comparisons The NOT operator should be placed next to its operand. No spaces. e.g. !$value Parenthesis should be used for grouping, especially with multiple comparisons. e.g. if (($val1 == $val2 ) && (val2 == $val3))
13.
Strings Use spaces around string concating. eg: 'str ' . $value . ' str2'; Use ' instead of " if there are no variables or special characters.
14.
Comments Use // for commenting code during development Use /* */ for block commenting unless you nest /* */ comments. Mark bug-fix changes to production code with date and initials, e.g. //20051225 AB
15.
PHP DOC Strings Add appropriate PHP DOC Strings to code Every file should have the doc string of this format at the top /** * Purpose of the file * @package Project Name or Module Name * @author Initials | Date * @todo Notes, If any */
16.
PHP DOC Stringse.g. /** * Handles authentication * @package Mytoday * @author AB | 20070112 * @todo Need to add password encryption */
17.
PHP DOC StringsFunctions need to have doc strings like : /** * purpose of the function * @param ParameterName1 Content of Param1 * @param ParameterName2 Content of Param 2 * @return Return Type Returned Content * @author Initials | Date * @todo Notes, if any */
18.
PHP DOC Strings/** * Fetches and Returns User Details * @param $user_id User ID * @param $user_type Type of User (1 – Admin, 2 - Employee and 3 - Customer) * @return Array User details array, if found, * @author AB | 20070112 * @todo Need to optimize info returned */