PHP Programming Style specification Share _php Foundation

Source: Internet
Author: User
Tags array definition logical operators lowercase php code php programming sql injection strlen

Description: This specification by Easychen for reference by SINA Network application Development Department "C + + Development Code" and interactive technology department "PHP4 Development Code", as well as the phpdocument specifications of the development norms. I feel very good, suitable for the development of PHP, for everyone's reference, to develop a good programming style is very necessary.

1th Chapter Naming norms

1.1 Variables

1.1.1 Global Variables

Global variables start with $g_, such as $g_data_list.

1.1.2 General Variable

A generic variable is named with a lowercase letter, separated by an underscore between words.

Variable names should be used in the form of nouns or adjectives + nouns. such as $value, $new _value.

1.1.3 Temporary variable

Do not use temporary variables such as $i, $J, etc. that are frequently used in loops for other purposes.

1.2 function

Functions are named in lowercase letters, and words are separated by underscores.

The naming of functions suggests using verbs + nouns, such as get_user_img.

Functions that complete a set of functions are put into a file, and the file that holds the function is named after function_name.func.php.

1.3 Class

Class uses the English case to separate the words, including the first word, the first letter of all words, such as PageManager;

In a class, the method is placed in front of the attribute definition, and the public method is placed before the private method;

In general, a class corresponds to a file;

When some classes are closely related, they can be stored in a file;

The file that holds the class is named in ClassName.class.php mode.

1.4 Method

Method uses the English case to separate the words, except the first word, the first letter of the other words capitalized, such as Getcurrentpage ();

Do not use abbreviations that are not commonly used, such as where2go ();

When using commonly used abbreviations, capitalize only the first letter, such as gethtml ().

2nd Chapter Layout Rules

2.1 Semantic separation

Each function and method should be separated by a blank line;

The lines that are tightly linked in the same function can be wrapped, and others need to be wrapped.

2.2 Space Rules

2.2.1 Logical operators must be preceded and preceded by spaces

That's right

Copy Code code as follows:
$a = = $b;

Error

Copy Code code as follows:

$a = = $b;
$a = = $b;

That's right

Copy Code code as follows:
$a + +; $a--;

Error

Copy Code code as follows:
$a + +; $a--;

Note adding one minus one operator does not add space.

2.2. More than 2 parameters must be separated with spaces
That's right

Copy Code code as follows:

$g _pro, $g _user, g_show;
Get_db_info ($host, $user, $passwd);

Error

Copy Code code as follows:

$g _pro, $g _user, $g _show;
Get_db_info ($host, $user, $passwd);

You must add a space after the 2.2.3 syntax keyword

For example: If, for, while, switch ....
That's right

Copy Code code as follows:
for ($i = 0; $i < $i + +)

Error

Copy Code code as follows:
for ($i = 0; $i < $i + +)

2.3 String and variable join rules
string is connected to the variable using '. ' Number, must be in '. ' You must add "{}" before and after the variable by using the "number".
That's right

Copy Code code as follows:

$my _name = ' file_ '. $var 1;
$my _name = "file_{$var 1}";

Error

Copy Code code as follows:

$my _name = "File_ '. $var 1;
$my _name = "File_$var1";

2.4 Parentheses Rule
After the function name parentheses do not need to add a space, after the syntax keyword parentheses must add space.
That's right

Copy Code code as follows:
for ($i = 0; $i < $i + +)
strlen ($my _name);

Error

Copy Code code as follows:
for ($i = 0; $i < $i + +)
strlen ($my _name);

2.5 curly Braces Rules
Curly braces must correspond to up and down.

That's right

Copy Code code as follows:

if ($a)
{
$b = $a;
}

Error

Copy Code code as follows:

if ($a) {
$b = $a;
}

2.6 Array Definition Rule

There must be a single quotation mark before and after the key value in the array definition and use.
PHP Code:
That's right

Copy Code code as follows:

Array (' name ' => ', ' Gender ' => ');
$user _info[' name '];

Error

Copy Code code as follows:

Array (name => ', Gender => ');
$user _info[name];

2.7 SQL rules

The SQL statement keywords embedded in PHP are all capitalized;
The table name and field name should be enclosed with an inverted quotation mark (') to prevent an error because the field name contains spaces;
The data values are enclosed in single quotes ', and you should ensure that the single quotes in the data values are escaped to prevent SQL injection.

That's right

Copy Code code as follows:
$sql = "Select ' user '. ' Name ' from ' user ' WHERE ' id ' = ' $id ' LIMIT 1";

Error

Copy Code code as follows:
$sql = "Select Name.user from name where id = $id";

3rd Chapter Rules of annotations

3.1 General rules
Do not write unnecessary annotations; only when the code does not explain the logic well, it is supplemented with annotations;
Take notes as part of the program and write/maintain the annotations while writing/maintaining the code;
Note The Phpdocumentor specification is fully used to facilitate the generation of API-level documentation.

3.2 Detailed rules
Please see the Phpdocumentor Manual. Below gives a demonstration of the various parts of the annotation.

3.2.1 Copyright Information
Note Name Copyright information
Note Demo:

Copy Code code as follows:

//
// +----------------------------------------------------+
// | Phpdocumentor |
// +----------------------------------------------------+
// | Copyright (c) 2000-2003 Joshua Eichorn |
// | Email jeichorn@phpdoc.org |
// | Web http://www.phpdoc.org |
// +----------------------------------------------------+
// | This source file is subject to PHP License |
// +----------------------------------------------------+
//

Note Use//to mark copyright information to avoid conflict with Phpdocumentor's Page-level DocBlock

3.2.2 File Header Annotation Example

Note Name File header Comment
Note Demo:

PHP Code:

Copy Code code as follows:

/**
* All abstract representations of inline tags are into this file
* @package Phpdocumentor
* @subpackage Inlinetags
* @since separate file since version 1.2
* @version $Id $
*/

Note
1 The file header annotation needs to indicate the package and the child package that belongs to;
2 Add $id to @version to facilitate the use of CVS to manage files.

3.2.3 Class Annotation Example
Annotation Name Class Comment
Note Demo:

PHP Code:

Copy Code code as follows:

/**
* Use this element to represent a {@}inline tag} like {@}link}
* @see Parserstringwithinlinetags
* @package Phpdocumentor
* @subpackage Inlinetags
* @author Greg Beaver <cellog@users.sourceforge.net>
* @since 1.0RC1
* @version $Revision: 1.21.2.6 $
* @tutorial inlinetags.pkg
*/


3.2.4 Class Attribute Annotation Example
Annotation Name Class attribute comment
Note Demo:
PHP Code:

Copy Code code as follows:

/**
* Element Type
*
* Type is used by many functions to skip the hassle of
*
* <code>
* If Get_class ($blah) = = ' Parserblah '
* </code>
* Always "Inlinetag"
* @var String
*/
var $type = ' Inlinetag ';

3.2.5 function/class method annotation Example
Annotation Name function/class method comment
Note Demo:
PHP Code:

Copy Code code as follows:

/**
* @return string Always ' "
* Calculate the short description of a DocBlock
* @see parserstringwithinlinetags::getstring ()
* @see Parserstringwithinlinetags::trimmedstrlen ()
*/
function getString ()
{
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.