Note: This is the coding specification seen in the PHPCMS development documentation. Although it is called the PHPCMS development specification, I think all PHP programming should be like this. Having written so much PHP, I feel that a lot of coding is lacking in comparison with this standard. I must correct it in the future.
Phpcms coding specification
1. Introduction…. 2
2. Scope of application…. 2
3. The importance and benefits of standardization…. 3
4. PHP coding specification And principles…. 3
4.1. Code markup… 3
4.2. Comments… 3
4.3. Writing rules… 4
4.3.1. Indentation… 4
4.3.2. Large Brackets {}, if and switch. 4
4.3.3. Operators, parentheses, spaces, keywords and functions... 5
4.3.4. Function definition... 6
4.3.5. Quotes... 6
4.3.6. Multi-language issues... 7
4.4. Naming principles... 8
4.4.1. Variable, object, function names... 8
4.4.2. Constants... 8
4.5. Initialization and logic checking of variables... 8
4.6. Security... 9
4.7. Compatibility... 9
4.8. Code reuse... 10
4.9. Other details... 10
4.9.1. Contains calls... 10
4.9.2. Error reporting levels... 11
5. Database design... 11
5.1. Fields... 11
5.1.1. Table and field naming... 11
5.1.2. Field structure... 11
5.2. SQL statement... 12
5.3. Performance and efficiency... 13
5.3.1. Fixed-length and variable-length tables... 13
5.3 .2. Operation and retrieval... 13
5.3.3. Structure optimization and index optimization... 14
5.3.4. Query optimization... 14
5.3.5. Compatibility issues... 16
6. Template design… 16
6.1. Code markup… 16
6.2. Writing rules… 16
6.2.1. HTML. 16
6.2.2. Variables… 16
6.2.3. Language elements... 17
6.2.4. Indentation... 17
7. Files and directories... 17
7.1. File naming... 17
7.2. Directory naming... 18
7.3. Empty Table of Contents Index… 18
1. Introduction
This specification consists of programming principles, which integrates and refines the mature experience accumulated by developers over a long period of time, and is intended to help form a good and consistent programming style. In order to achieve twice the result with half the effort, this document will be updated from time to time if necessary.
Copyright: Shaanxi Jiushi Lulu Network Technology Co., Ltd., all rights reserved
Last updated: November 20, 2006
2. Scope of application
If no special instructions are given, the following The rule requirements are fully applicable to the phpcms project, and can also be applied to most other PHP projects in the company.
3. The importance and benefits of standardization
When a software project tries to comply with public and consistent standards, it can make it easier for developers participating in the project to understand the code in the project and clarify the status of the program. It allows new participants to quickly adapt to the environment and prevents some participants from creating their own style and developing lifelong habits out of the need to save time, causing others to waste too much time and energy when reading. And in a consistent environment, the chance of coding errors can also be reduced. The disadvantage is that everyone has different standards, so it takes a while to adapt to and change your coding style, which temporarily reduces work efficiency. Considering the long-term healthy development of the project and higher team work efficiency in the later period, it is worthwhile to consider the temporary reduction in work efficiency, and it is also a process that must be gone through. Standards are not the key to project success, but they can help us be more efficient in team collaboration and complete set tasks more smoothly.
1. Programmers can understand any code and understand the status of the program
2. Newcomers can quickly adapt to the environment
3. Prevent people new to PHP from creating their own code out of the need to save time. Establish a style and develop lifelong habits
4. Prevent people new to PHP from making the same mistakes over and over again
5. In a consistent environment, people can reduce the chance of making mistakes
6. Programmers We have a common enemy
4. PHP coding standards and principles
4.1. Code tags
PHP programs can use or to define PHP code, when embedding pure variables in HTML pages , you can use this form.
In recent years, the PHP development team has been advocating code standardization and standardization. In future versions of PHP, it may start to deprecate or even cancel this shorthand form. Therefore, in order to enhance program compatibility, we will unify it before releasing it.
4.2. Comments
Comments are to add short introductory content to codes whose functions are easy to forget. Please use C-style comments "/* */" and standard C++ comments "//".
It is inevitable to leave some temporary code and debugging code during program development. Such code must be commented to avoid being forgotten in the future. All temporary, debugging, and experimental code must add a unified comment mark "//debug" followed by complete comment information. This makes it easier to batch check whether there are any doubtful problems in the program before the program is released and final debugging. code. For example:
$num = 1;
$flag = TRUE; //debug It is not sure whether $flag needs to be assigned a value here
if(empty($flag)) {
//Statements
}
4.3. Writing Rules
4.3.1. Indentation
The unit of each indentation is one TAB (8 blank characters wide), everyone needs to participate The developers of the project make mandatory settings in the editor (UltraEdit, EditPlus, Zend Studio, etc.) to prevent formatting irregularities caused by forgetting when writing code.
This indentation specification applies to functions, classes, logical structures, loops, etc. in PHP and JavaScript.
4.3.2. Braces {}, if and switch
The first bracket is in the same column as the keyword, and the last bracket is in the same column as the keyword;
In the if structure, if and elseif are with the two circles before and after The brackets should be on the same line, with a space on the left and right, and all curly brackets should be on a separate line. In addition, even if there is only one line of statement after the if, curly brackets still need to be added to ensure a clear structure; in the
switch structure, usually when a case block is processed, the subsequent case block processing will be skipped, so in most cases it is necessary Add break. The position of break depends on the program logic. It can be on the same line as case or on a new line. However, in the same switch body, the position format of break should be consistent.
The following are examples that comply with the above specifications:
If ($condition)
{
switch ($var)
{
case 1: echo 'var is 1'; break;
case 2: echo 'var is 2'; break;
default: echo 'var is neither 1 or 2'; break;
}
}
else
{
switch ($str)
{
case 'abc':
$result = 'abc';
break;
default:
$result = 'unknown';
break;
}
}
4.3.3. Operators, parentheses, spaces, keywords and functions
Each operator is between the values or expressions involved in the operation on both sides There must be a space. The only exception is that there are no spaces on both sides of the character concatenation operator;
The left bracket "(" should be closely together with the function keyword. Otherwise, a space should be used to separate the "(" from the previous content. ;
Except for the right bracket ")" followed by ")" or ".", all other brackets are separated by spaces;
Unless specifically required in strings, under normal circumstances, they do not appear in programs and HTML Two consecutive spaces;
Under any circumstances, blank lines with TAB or spaces cannot appear in PHP programs, that is: such blank lines should not contain any TAB or spaces. At the same time, no extra TAB or spaces can appear at the end of any program line. Most editors have the function of automatically removing spaces at the end of lines. If the habit is not developed well, you can use it temporarily to avoid unnecessary spaces;
For each large program body, blank lines should be added above and below, two Only use 1 blank line between program blocks, multiple lines are prohibited.
Try to divide the program blocks as reasonably as possible. Too large or too small divisions will affect other people’s reading and understanding of the code. Generally, it can be divided by larger function definition, logical structure, and functional structure. Program blocks with less than 15 lines do not need to add blank lines;
In the description or display part, if the content contains mixed Chinese, numbers, and English words, spaces should be added before and after the numbers or English words.
Based on the above principles, the following examples illustrate the correct writing format:
$result = (($a + 1) * 3 / 2 + $num)).'Test';
$condition ? func1($var) : func2($var);
$condition ? $long_statement
: $another_long_statement;
if ($flag)
{
//Statements
//More than 15 lines
}
Showmessage('Please use restore.php tool to restore data.');