Name the hump structure, which starts with a lowercase letter, starting with the second word, capitalized with the first letter of each word.
Large half day time collation of PHP coding specifications ...
First, the naming specification:
1. Naming the constants
1) Letters, all uppercase, use "_" to separate words.
such as: DEFINE ("Php_demo_print", "HEllo World");
2. Naming of variables
1) Name The hump structure, which starts with a lowercase letter, starting with the second word, capitalized with the first letter of each word.
such as: $getElement = "MYODBC";
2) Global variables start with "g_" and non-global variables do not start with "G_"
such as: $g _i;
3) All "temporary" variables must be unset after they are used.
4) does not allow letters to appear exactly the same, only the case of different names.
5) do not allow underscores at the end of a name.
6) do not appear single-letter variables, such as i,j,k (only in the control of the number of loops and the number of loop codes within 3, you can consider using)
7) The number cannot appear in the variable name.
8) It is best to indicate whether a variable name is a numeric or string variable.
For example: $myCount _i represents a numeric variable, $myString _s represents a string.
9) If there is a calculation qualifier, add the qualifier to the last name.
such as: $calTimeStart, $calTimeEnd.
3. Naming the function
1) function names are all lowercase and can be underlined between words.
2) The function name of the Access object is in the form of set_xxx () and get_xxx ().
3) The function name of the Boolean type can be used in the form isxxx ().
4) The number cannot appear in the function name.
4. Class naming
1) class name with the first letter capitalized in English normal word order accurately describes the meaning of the class. such as: Employee.
2) The method naming rules of a class are consistent with the naming conventions of functions.
3) The property name of the class is in full letter lowercase.
5. Naming of files
1) file name characters are all lowercase
2) The file name should reflect its content and function.
such as: storage of classes of files, name.class.php; The file containing the function can be named with name.func.php or name.inc.php; config file, name.cfg.php or config.inc.php.
Second, the annotation specification:
1. Notes for variables
1) The variable's peers are followed by a blank space using a double slash comment.
such as: $myDemo = ' a demo '; This is an in-line comment
2. Comment on function and function body
/*
@xxxx, BBB
*/
Describe function and return values and reference methods for functions
function ff (AAA, BBB) {
......
A single line comment in the code for common inclusions
......
return false;
}
3. Notes to the file
1) file function, creator information, time.
2) Known bugs. Of course, it is best to fix all errors, but there may be no way to fix the errors or temporarily have no energy to modify
3) Develop and maintain a historical list of files. Record the author, date, and modified content of each modification
4) The main algorithm must be explained, the main process must give the guidance of the description
5) Standard format
| /**
* @description describe the function, purpose, existing bug, design method of the class.
* @author Author's name
* @version Last Modified date (date format suggested by: year-month-day format, for example: 2008-04-05)
*/
If you have modified a versioned class, you need to modify the additional modification history for each modification in the following format:
* @modify
* Modified person + Modified date (date format suggested by: year-month-day format, for example: 2008-04-05)
* Revision Instructions
|
Three, code writing style
1. General principles
1) A line of code is as short as possible, and one line of code is guaranteed to do something.
2) Use parentheses to define the order of operations. Don't let others judge complex operational priorities.
3) Rational use of empty lines, divided into logical processing.
4) indent the code using the TAB key (set four spaces).
5) It is recommended to follow the 30 second principle. If another programmer is unable to understand what your function is doing in 30 seconds, how and why, it means that your code is difficult to maintain and must be improved.
2. Control structure
1) IF Specification:
1. If next to else, another line
2. Place the contrast constant on the left side of the equation
3. Branch to add a comment, preceded by a (TAB) indent
if (6 = = $errorNum) {//NOTE 1
..
} else {
if (5 = = $num) {//NOTE 2
...
} else {
...
}
}
2) Switch Specification:
Switch (...) {
Case 1://branch 1 Comment
...
There is no break, go directly to the next case, need to add comments
Case 2: {//branch 2 Comment
$v = Get_week_number (); Create variable, in {}
...
}
Break
Default
}
3) for specification:
for ($i =1; $i <=3; $i + +) {
......
}
4) While specification:
while ($i <=5) {
....
}
5) Foreach Specification:
Foreach ($arr as $v = = $k) {
....
}
Four, code writing style
1. Use $row[' ID '] method to fetch data, do not use $row[id].
2. Pure strings are contained using single quotes.
3. Delete unused variables
4. Determine the maximum number of loops before executing the For loop, and do not calculate the maximum value once per loop.
5. Inside the loop do not declare variables, especially large variables: objects (language Common).
6. Use the Strpos () function to determine if there is a replacement function before executing the string substitution.
7. Use foreach instead of while to iterate through the array. foreach is significantly more efficient when iterating through arrays than while loops
8. The Str_replace function is faster than the Preg_replace function, but the STRTR function is four times times more efficient than the Str_replace function.
9. When the variable $i is incremented or decremented, $i + + will be slower $i than the + +. This difference is PHP-specific
10. Masking error messages with @ is very inefficient.
(turn) Detailed PHP coding specification document