Some PHP programming specifications

Source: Internet
Author: User
Tags array definition


From: http://blog.csdn.net/youacai/article/details/6291422

I have been writing PHP in the style of PHP functions for a long time. All variables, functions, and classes use lower-case letters, and the words are separated by the following strikethroughs, which have always been relatively difficult to use.CodeStandard, I personally think it is very troublesome to write code between uppercase and lowercase letters, and PHP's own functions are all in lower case. Why don't I use this format!

Good code writing habits + good comments habits + php?entor =ProgramManual

A team must have a neat coding habit. If it is coupled with a unified IDE development environment, detailed task coding procedures, and complete code testing (such as simpletest ), the development efficiency of the entire team will be greatly improved.

Note: easychen draws on the Sina Network Application Development Department C ++ development specifications, the Interaction Technology Department PhP4 development specifications, and the phpdocument development specifications. I think it is very good and suitable for PHP development. for your reference, it is very necessary to develop a good programming style. (Reprinted)

Chapter 2 naming conventions 1st variables 1.1.1 global variables
Global variables start with $ g _, for example, $ g_data_list.

1.1.2 General Variables
Generally, variables are named by lowercase letters, and words are separated by underscores.
Variable names should use nouns, adjectives, and nouns. For example, $ value and $ new_value.

1.1.3 temporary variables
Do not use temporary variables such as $ I and $ J that are frequently used in loops for other purposes.

Generally, temporary string variables are represented by $ C, $ D, and $ E. Temporary numeric variables are represented by $ I, $ J, $ m, and $ n.

1.2 Functions
The function is named by lowercase letters, and words are separated by underscores.
We recommend that you use the verb + noun Method for function naming, such as get_user_img.
The functions that complete a set of functions are put into a file, and the files that store the functions are named by function_name.func.php.

1.3 categories
Classes use English uppercase and lowercase letters to separate words, including the first word, and uppercase letters of all words, such as pagemanager;
In the class, the method is placed in the attribute definition front edge, and the public method is placed in the special method front edge;
Generally, a class corresponds to a file;
When some classes are closely related, they can be stored in a file;
Class files are named in classname. Class. php format.

1.4 Method
Use English case to separate words. Except the first word, the first letter of other words is capitalized, such as getcurrentpage ();
Do not use uncommon abbreviations, such as where2go ();
When common abbreviations are used, only the first letter is capitalized, such as gethtml ().

Chapter 3 layout rules

2.1 semantic Separation
Empty row intervals should be used between functions and methods;
Line breaks are not allowed between closely related statements in the same function. In other cases, line breaks are required.

2.2 space rules

2.2.1 spaces must be added before and after logical operators
Correct $ A ==$ B;
Error $ A = $ B;
$ A = $ B;
Remarks-

Correct $ A ++; $ -;
Error $ A ++; $ -;
The note plus minus one operator cannot contain spaces.

2.2.2 separate multiple parameters with spaces
Correct: $ g_pro, $ g_user, g_show;
Get_db_info ($ host, $ user, $ passwd );
Error $ g_pro, $ g_user, $ g_show;
Get_db_info ($ host, $ user, $ passwd );
Remarks-

2.2.3 spaces must be added after the syntax keyword
For example: if, for, while, switch .....

Correct for ($ I = 0; $ I <10; $ I ++)
Error for ($ I = 0; $ I <10; $ I ++)
Remarks-

2.3 string and variable connection rules
When the '.' sign is used for string-to-variable connection, spaces must be added before and after '.'. When the 'sign is used,' {} 'must be added before and after the variable "{}".

Correct $ my_name = 'file _ '. $ var1;
$ My_name = "file _ {$ var1 }";
Error $ my_name = "file _ '. $ var1;
$ My_name = "file _ $ var1 ″;
Remarks-

2.4 parentheses
You do not need to add spaces to the brackets behind the function name or to the brackets after the syntax keyword.

Correct for ($ I = 0; $ I <10; $ I ++)
Strlen ($ my_name );
Error for ($ I = 0; $ I <10; $ I ++)
Strlen ($ my_name );
Remarks-

2.5 curly brackets
Curly braces must be upper and lower matched.

Correct
If ($)
{
$ B = $;
}

Error if ($ ){
$ B = $;
}

Note-I personally think both methods are acceptable, and many people use the second method.

2.6 array definition rules

Single quotation marks must be added before and after the key value in array definition and usage.

PHP code:

Download: php_array.php // correct
Array (
'Name' => 'd5s. cn ',
'Gender' => 'php'
);
// Error
Array (
Name => 'd5s. cn ',
Gender => 'php'
);
?>

2.7 SQL rules

All SQL statement keywords embedded in PHP are in uppercase;
The table name and field name must be enclosed in reverse quotation marks (') to prevent errors because the field name contains spaces;
The data values are enclosed in single quotes, and the single quotes in the data values must be escaped to prevent SQL injection.

Correct $ SQL = "select 'user'. 'name' from 'user' where 'id' = '$ id' limit 1 ″;
Error $ SQL = "select name. User From name where id = $ ID ";
Remarks-

Chapter 4 comment rules

3.1 General Rules
Unnecessary comments are not written; comments are used only when the Code cannot describe the logic well;
Regard comments as part of a program and write/maintain comments when writing/maintaining code;
The annotation uses the phpdocumentor specification to generate API-level documents.

3.2 Detailed Rules
See phpdocumentor manual. The comments and examples of each part are given below.

3.2.1 copyright information
Annotation name copyright information
Annotation demonstration //
// + ------------------ +
// | Phpdocumentor |
// + ------------------ +
// | Copyright (c) 2000-2003 Joshua eichorn |
// | Email jeichorn@phpdoc.org |
// | Http://www.phpdoc.org |
// + ------------------ +
// | This source file is subject to PhP license |
// + ------------------ +
//
Note: Use // to indicate the copyright information to avoid conflict with the page-level docblock of phpdocumentor.

3.2.2 File Header annotation example

Annotation name file header Annotation
Annotation demonstration

Download: php_doc.php * all abstract representations of inline tags are in this file
* @ Package phpdocumentor
* @ Subpackage inlinetags
* @ Since separate file since version 1.2
* @ Version $ ID $
*/
?>

Remarks
1. Specify the package and sub-package to which the file header comment belongs.
2 add $ ID to @ version to facilitate file management using CVS

3.2.3 class annotation example
Annotation name class Annotation
Annotation demonstration

Download: php_class.php /**
* Use this element to represent an {} inline tag} like {} link}
* @ See parserstringwithinlinetags
* @ Package phpdocumentor
* @ Subpackage inlinetags
* @ Author Greg beaver
* @ Since 1.0rc1
* @ Version $ revision: 1.21.2.6 $
* @ Tutorial inlinetags. PKG
*/
?>

Remarks-

3.2.4 example of class property Annotation
Annotation name class attribute Annotation
Annotation demonstration

Download: php. php/** var $ type = 'inlinetag ';
* Element type
*
* Type is used by using functions to skip the hassystemic
*
*
* If get_class ($ blah) = 'parserbla'
*

* Always "inlinetag"
* @ Var string
*/
?>

Remarks-

3.2.5 function/class method annotation example
Annotation name function/class method Annotation
Annotation demonstration

Download: php. php/** function getstring ()'';
* @ Return string always''
* Calculate the short description of a docblock
* @ See parserstringwithinlinetags: getstring ()
* @ See parserstringwithinlinetags: trimmedstrlen ()
*/

{
Return
}
?>

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.