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 Standards
1. Introduction… 2
2. Scope of application… 2
3. The importance and benefits of standardization… 3
4. PHP coding specifications 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. Braces {}, if and switch. 4
4.3.3. Operators, parentheses, spaces, keys Words and functions… 5
4.3.4. Function definition… 6
4.3.5. Quotation marks… 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. Fields 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 indexing 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 directory 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 update date: November 20, 2006
2. Scope of application
If no special instructions are given, the following rules and requirements are fully applicable to the phpcms project, and can also be used Most of them apply to 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 established 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. It prevents new people who are new to PHP from creating their own style and developing it for life out of the need to save time. Habits
4. Prevent people who are new to PHP from making the same mistakes again and again
5. In a consistent environment, people can reduce the chance of making mistakes
6. Programmers have a consistent enemy
4. PHP coding standards With Principle
4.1. Code markup
PHP programs can use or to define PHP code. This form can be used when embedding pure variables in HTML pages.
In recent years, the PHP development team has been advocating code normalization and standardization. In future versions of PHP, this shorthand form may be deprecated or even canceled. Therefore, in order to enhance program compatibility, we will unify
4.2. Comments
Comments before release It is to add short introductory content to the code that is easy to forget its function. 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 forgetting it 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), which needs to be set by each developer participating in the project 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 goes with the keyword, and the last bracket goes with the keyword in the same column;
In the if structure, if and elseif go with the two parentheses before and after, one space on the left and right, all Braces are placed on a separate line. In addition, even if there is only one line of statement after the if, braces 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 to add a 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 neitherither 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 must be separated from the values or expressions involved in the operation on both sides There is 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 connected with the function keyword. Otherwise, a space should be used to separate the "(" from the previous content;
Right bracket Except for brackets ")" followed by ")" or ".", all other brackets are separated by spaces;
Unless specifically required in strings, under normal circumstances, two consecutive spaces do not appear in programs and HTML;
Under no circumstances should blank lines with TAB or spaces 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, a blank line and two program blocks should be added above and below Only use one blank line between them, multiple lines are prohibited.
Divide the program blocks as reasonably as possible. Too large or too small divisions will affect others' 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 the restore.php tool to restore data.' );
Current page 1/3 123Next page
The above introduces the 1/3 page of the very good PHP coding specification at http://67.220.92.14/forum/inde, including the content at http://67.220.92.14/forum/inde. I hope it will be helpful to the PHP tutorial. Interested friends help.