That's very nice PHP code specification 1th/3 page _php Tips

Source: Internet
Author: User
Tags function definition php and php code php programming
Note: This is the code specification from the PHPCMS development document, although the development specification is called PHPCMS, but I think all PHP programming should be so. Write so much php, a lot of coding compared to this specification is a lot of lack of, in the future must be compared to correct.

PHPCMS Coding Specification
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 tags ... 3
4.2. Note ... 3
4.3. Writing rules ... 4
4.3.1. Indent ... 4
4.3.2. Curly braces {}, 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 problem ... 7
4.4. Principle of nomenclature ... 8
4.4.1. Variable, object, function name ... 8
4.4.2. Constant ... 8
4.5. Initialization of variables and logic checking ... 8
4.6. Safety ... 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 Reporting 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 statement ... 12
5.3. Performance and efficiency ... 13
5.3.1. Fixed length and variable length table ... 13
5.3.2. Operation and Retrieval ... 13
5.3.3. Structural optimization and index optimization ... 14
5.3.4. Query optimization ... 14
5.3.5. Compatibility issue ... 16
6. Template design .... 16
6.1. Code tags ... 16
6.2. Writing rules ... 16
6.2.1. HTML. 16
6.2.2. Variable ... 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 naming ... 18
7.3. Empty Directory Index ... 18

1. Introduction
This specification, which is composed of programming principles, blends and refines the mature experience that developers have accumulated over a long period of time, and is intended to help form a good and consistent programming style. To achieve a multiplier effect, if necessary this document will be updated irregularly.
Copyright: Shaanxi Jiu-LU Network Technology Co., Ltd., all rights reserved
Last modified: November 20 2006

2. Scope of application
In the absence of special instructions, the following rules are required to be fully applicable to the PHPCMS project and are also applicable to most of the company's other PHP projects.

3. Importance and benefits of standardization
When a software project tries to comply with common standards, it makes it easier for developers involved in the project to understand the code in the project, and to clarify the status of the program. So that new players can quickly adapt to the environment, to prevent some participants from the need to save time, create a style and develop a lifelong habit, resulting in other people in the reading of waste too much time and energy. And in a consistent environment, you can also reduce the chance of coding errors. The flaw is because everyone's standard is different, so need a period of time to adapt and change their coding style, temporary drop bottom of the work efficiency. It is worthwhile to consider a temporary reduction in productivity from the development of the long-term health of the project and the higher efficiency of the team in the later period, which is a process that must be passed. Standards are not the key to the success of a project, but they can help us to be more efficient in team work and more successful in accomplishing our assigned tasks.
1. Programmers can understand any code and find out the status of the program
2. Newcomers can quickly adapt to the environment
3. Prevent new contacts with PHP from the need to save time, create a style and develop a lifelong habit
4. Prevent new contact with PHP people make the same mistake 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 Specifications and principles

4.1. Code tags
PHP programs can use or to define the PHP code, the HTML page embedded in the pure variable, you can use this form.
In recent years, the PHP development group has been advocating code standardization and standardization, the future version of PHP may start not recommended to use or even cancel and this shorthand form, so in order to enhance program compatibility, before the release we will unify

4.2. Note
Annotations Add short introductory content to code that is easy to forget. Please use the C-style annotation "/* *" and the standard C + + annotation "//".

There are some temporary code and debugging code that must be added to the program development to avoid future oblivion. For all the temporary, debug, and Experimental code, you must add a unified annotation tag "//debug" followed by complete annotation information, so that you can easily check whether there are any questionable code in the program before the program is published and eventually debugged. For example:
$num = 1;
$flag = TRUE; Debug here is not sure if you need to assign a value to $flag
if (empty ($flag)) {
Statements
}

4.3. Rules of writing

4.3.1. Indentation
The Unit convention for each indentation is a tab (8 blank character widths), and each developer involved in the project is forced to set up in the editor (UltraEdit, EditPlus, Zend Studio, and so on) to prevent the formatting from being forgotten when the code is written.
This indentation specification is applicable to functions, classes, logical structures, loops, and so on in PHP and JavaScript.

4.3.2. Curly braces {}, if, and switch
The first bracket is the same as the keyword, and the end bracket is in the same column as the key word;
In the IF structure, if and ElseIf are peers with two parentheses around each other, one space, and all curly braces are separate lines. In addition, even if there is only one line of statements after if, you still need to add braces to ensure a clear structure;
In a switch structure, when a case block is processed, the case block is skipped after it is processed, so in most cases you need to add a break. The location of the break depends on the program logic, with the case on one line, or a new line, but the position format of the break should be consistent in the same switch.
The following are examples of compliance with the above specification:
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 has a space between the value or expression that participates in the operation on both sides, and the only special case is the character concatenation operation symbol with no spaces on either side;
The left parenthesis "(" should be attached to the function keyword, in addition to the "(") should be used to separate "(") with the previous content;
Closing parenthesis ")" except for the following "") "or". " , others are separated by spaces;
In addition to the string of deliberately required, in general, in the program and HTML does not appear two consecutive spaces;
In any case, a blank line with a tab or a space cannot appear in a PHP program, i.e., such blank lines should not contain any tab or space. Also, no extra tab or space can appear at the end of any program line. Most editors have the function of automatically removing the trailing space, if the habit is not good, it can be used temporarily, avoid extra space to produce;
For each larger program body, a blank line should be added to the top and bottom, with only 1 blank rows between two blocks, and multiple rows prohibited.
The partition of the program block is as reasonable as possible, too large or too small segmentation will affect others to read and understand the code. Generally, it can be divided into large function definition, logical structure and functional structure. Less than 15 lines of the program block, can not add up and down blank lines;
In the description or display section, content such as Chinese, digital, English words mixed, should be in the number or English words before and after adding spaces.

According to 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 lines
}
ShowMessage (' Please use the restore.php tool to recover the data. ');
Current 1/3 page 123 Next read the full text

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.