or a team to work effectively and to share ownership of all the code, all the programmers within the team need to write the code in the same way or same programming style.These programming style refers to a set of guidelines,rules that one should follow in the team.programming style used in a particular program may be derived from the coding standards or code convention of a company/organization.
Good coding standards are important in any development project, particularly when multiple developers are working on the same project. Having coding standards helps to ensure that the code is of high quality, has fewer bugs, and is easily maintained.here are coding standered and convention that one should follow while developing a PHP program.
Comments convention
- each php file should have Header comment block.This block should contains short description of file,category,package,auther,coyright etc.
- each function block should have Function comment block.This bloc contains brief description about task perform by function,parameter passed and data type n identifier which function returns.
- Inline comment can be helpful in case of complex logical block of code.
Naming convention
- Identifier names should be explanatory, and should be in word-case. Also the first letter of the variable or parameter or IDs must tell the nature thereof.
-
- For Example
Class: ClassName
Object: oObjectName
Function Name: functionName()
Private Method: _MethodName()
Constants: CONSTANTNAME
String Variable: sVariableName
Array Variable: aVariableName
Integer Variable:$ iVariableName
Float Variable: fVariableName
Boolean Variable: bVariableName
- For Example
- Global variable should contain package name in uppercase followed by 1 underscore and identifier name.
For Example
-
Coding convention
- Use an indent of 4 spaces, with no tabs.This helps to avoid problems with diffs, patches, SVN history.
- lines at approximately 75-85 characters long for better code readability.
- Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.
- Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon.
- For readability there should be one space on either side of an equals sign used to assign the return value of a function to a variable.
- Always use <?php ?> to delimit PHP code, not the <? ?> shorthand.
- Argument in the function with default values go at the end of the argument list.
- Use include_once ‘path/of/php/file.php’ since it’s language construct it should not contains parentheses.
- While writing MYSql queries use UPPERCASE for keywords of query,back ticks for fieldnames.
- For Example
- $sQery = ‘SELECT `LeadId`,`name` FROM tblLeads WHERE `id`=4267534
- For Example