Very good PHP code specification. Page 1/3

Source: Internet
Author: User

Note: This shows the encoding specification from the PHPCMS development documentation. Although it is called the PHPCMS development specification, I think this is the case for all PHP programming. After writing so many PHP codes, many codes may not be able to compare and correct the codes in the future.

Phpcms coding Specification
1. Introduction .... 2
2. Applicability .... 2
3. importance and benefits of standardization .... 3
4. PHP coding specifications and principles .... 3
4.1. code mark... 3
4.2. comment... 3
4.3. Writing rules... 4
4.3.1. indent... 4
4.3.2. braces {}, if, and switch. 4
4.3.3. Operators, Parentheses, spaces, keywords and functions... 5
4.3.4. function definition... 6
4.3.5. quotation marks... 6
4.3.6. multi-language problems... 7
4.4. Naming principles... 8
4.4.1. variable, object, function name... 8
4.4.2. Constant... 8
4.5. Variable initialization and logic check... 8
4.6. Security... 9
4.7. Compatibility... 9
4.8. code reuse... 10
4.9. Other details... 10
4.9.1. Include call... 10
4.9.2. Error Report level... 11
5. database design .... 11
5.1. Field... 11
5.1.1. Table and field naming... 11
5.1.2. field structure... 11
5.2. SQL statements... 12
5.3. performance and efficiency... 13
5.3.1. Fixed Length and variable length table... 13
5.3.2. Calculation 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 mark... 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. indent... 17
7. Files And Directories .... 17
7.1. File Name... 17
7.2. directory name... 18
7.3. Empty Directory Index... 18

1. Introduction
This specification is composed of programming principles. It integrates and refines the mature experience accumulated by developers for a long time and is intended to help build a consistent programming style. This document will be updated occasionally if necessary to get twice the result with half the effort.
Copyright: Shaanxi luolu Network Technology Co., Ltd. All Rights Reserved
Last Updated: November 20-2006

2. Applicability
Unless otherwise stated, the following rules are applicable to phpcms projects and most of them are applicable to other PHP projects in the company.

3. importance and benefits of Standardization
When a software project tries to comply with public consistency standards, it makes it easier for the developer involved in the project to understand the code in the project and understand the program status. This allows new participants to quickly adapt to the environment and prevents some participants from creating a set of styles and forming a habit for life out of time-saving needs, this results in other people wasting too much time and energy reading. In a consistent environment, encoding errors can also be reduced. The defect is that each person's standards are different, so it takes some time to adapt to and change their encoding style, temporarily reducing work efficiency. It is worthwhile to consider the temporary reduction of work efficiency from the long-term healthy development of the project and the higher team efficiency in the future. It is also a necessary process. Standards are not the key to a successful project, but they can help us achieve higher efficiency in our team collaboration and accomplish our tasks more smoothly.
1. programmers can understand any code and find out the program status.
2. newcomers can quickly adapt to the environment
3. Prevent New PHP contacts from creating a new style and forming a habit for life out of time-saving needs.
4. Prevent New PHP contacts from making the same mistake again and again
5. In a consistent environment, people can reduce the chance of making mistakes.
6. programmers have the same enemy

4. PHP coding specifications and principles

4. 1. code mark
PHP programs can use or define PHP code. This form can be used when pure variables are embedded in HTML pages.
In recent years, the PHP development team has been advocating code standardization and standardization. PHP may not be recommended or even canceled in future versions. Therefore, to enhance program compatibility, We will unify

4. 2. Notes
Annotations are used to add brief introductions to codes that are easy to forget. Use the C-style comments "/**/" and Standard C ++ comments "//".

Some temporary code and debugging code are inevitably left in program development. Such code must be annotated to avoid future forgetting. For all temporary, debugging, and experimental code, you must add a unified annotation mark "// debug" followed by the complete annotation information, this allows you to check whether there are any problematic code in the program in batches before the program release and final debugging. For example:
$ Num = 1;
$ Flag = TRUE; // debug is not sure whether to assign a value to $ flag.
If (empty ($ flag )){
// Statements
}

4. Writing rules

4.3.1. indent
The Unit Convention for each indent is a TAB (8 blank characters in width). Each developer involved in the project needs to set it forcibly in the editor (UltraEdit, EditPlus, Zend Studio, etc, in case that the code is forgotten, the format is not standardized.
This indent specification applies to functions, classes, logical structures, loops, and so on in PHP and JavaScript.

4.3.2. braces {}, if, and switch
The Beginning bracket is the same as the keyword, and the end bracket is the same as the keyword;
In the if structure, if and elseif are in the same direction as the two parentheses, each with a space on the left and right, and all braces start with a separate line. In addition, even if there is only one line after the if statement, braces must be added to ensure the structure is clear;
In the switch structure, after a case block is processed, the skipped case block is processed. Therefore, break needs to be added in most cases. The position of break depends on the program logic. It can be the same line as case or 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 must have a space in the middle of the value or expression involved in the operation on both sides. The only special case is that no space is added on both sides of the character concatenation operator number;
The left parenthesis "(" should be closely related to the function keyword. In addition, space should be used to separate "(" from the preceding content;
Except for ")" or ".", brackets ")" are separated by spaces;
Except for the special needs of strings, generally, there are no consecutive spaces in the program and HTML;
Under no conditions can blank lines with tabs or spaces appear in the PHP program. That is, such blank lines should not contain any tabs or spaces. At the same time, no additional TAB or space is allowed at the end of any program line. Most editors have the function of automatically removing spaces at the end of a line. If you are not familiar with it, you can use it temporarily to avoid unnecessary spaces;
The upper and lower parts of a large program must be added with blank lines. Only one blank line is used between two program blocks, and multiple lines are prohibited.
The block division should be as reasonable as possible. too large or too small splitting will affect others' reading and understanding of the Code. Generally, it can be divided by large function definitions, logical structures, and functional structures. Blocks with less than 15 lines can be left blank;
In the description or display section, if the content contains Chinese characters, numbers, and English words, spaces should be added before and after the numbers or English words.

Based on the above principles, the following examples describe the correct writing format:
$ Result = ($ a + 1) x 3/2 + $ num). 'test ';
$ Condition? Func1 ($ var): func2 ($ var );
$ Condition? $ Long_statement
: $ Another_long_statement;
If ($ flag)
{

// Statements
// More than 15 lines
}
Showmessage ('use restore. php to restore data. ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.