PHP Programming Style specification sharing

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

  This specification by Easychen for reference to the Sina Network application development of the "C + + Development Code" and the interactive technology department "PHP4 Development Code", as well as 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

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 conventions   1.1 variables   1.1.1 Global variables   Global variables use $g_ to begin with, such as $g_data_list.   1.1.2 General variables   General variables are named with lowercase letters, 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. The   1.2 function   function is named in lowercase letters, separated by an underscore between words. The naming of   functions suggests the use of verbs + nouns, such as get_user_img.   functions to complete a set of functions into a file, the file that holds the function is named after function_name.func.php. The   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 the class, the method is placed in front of the attribute definition, the common method is placed before the private method, and   generally A class corresponds to a file;   When some classes are closely related, they can be stored in a file;   The file of the class is named in ClassName.class.php.   1.4 Method   method uses the English case to separate the words, except the first word, the first letter of the other words in uppercase, 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 interval;   The lines that are tightly linked in the same function can be wrapped, and others need to wrap.   2.2 Space Rules   2.2.1 Logical operators must be preceded before and after space   correct  Code as follows: $a = = $b;    Error     code as follows: $a = = $b; $a = = $b;     Correct     copy code code as follows: $a + +; $a--;    error     code as follows: $a + +; $a--;    Notes add one minus one operator without spaces.   2.2. More than 2 parameters must be separated with a space correct       code is as follows: $g _pro, $g _user, g_show;   Get_db_info ($host, $user, $passwd);      error     code is as follows: $g _pro, $g _user, $g _show;   Get_db_info ($host, $user, $passwd);      2.2.3 syntax keyword must be followed by space   For example: If, for, while, switch .... The correct       code is as follows: for ($i = 0; $i < $i + +)     error     code as follows: for ($i = 0; $i <; $i + + )      2.3 string and variable connection rule strings are used with variable connection '. ' Number, must be in '. ' You must add "{}" before and after the variable by using the "number". The correct   code is as follows: $my _name = ' file_ '. $var 1;   $my _name = "file_{$var 1}";      error     code as follows: $my _name = "File_". $var 1;   $my _name = "File_$var1";      2.4 Parentheses rule function name after parentheses do not need to add a space, the syntax of the parentheses after the keyword must add space. The correct     copy code code is as follows: for ($i = 0; $i < 10; $i + +)   strlen ($my _name);      error   code as follows: for ($i = 0; $i <; $i +)   strlen ($my _name);       2.5 curly brace rules curly braces must be up and down corresponding.   Correct   code as follows: if ($a)    {      $b = $a;    }      error   & nbsp Code as follows: if ($a) {        $b = $a;    }      2.6 Array definition rule   array definition and key value in use A single quote must be added before and after. PHP code: Correct     code as follows: Array (' name '   => ', ' Gender '  => ');    $user _info[' name '];      error     Copy code code as follows: Array (name => ', gender   &NBSP ;=> ');    $user _info[name];     2.7 SQL rules   SQL statement keywords embedded in PHP are all capitalized; The table name and field name are enclosed in quotation marks (') to prevent errors from including spaces in the field names; The data values are enclosed in single quotes '. You should also ensure that single quotes in the data values are escaped to prevent SQL injection.   Correct     copy code code as follows: $sql = "Select ' user '. ' Name ' from ' user ' WHERE ' id ' = ' $id ' LIMIT 1";    error &N Bsp   Copy code code as follows: $sql = "Select Name.user from name where id = $ID ";    3rd Comment rules   3.1 general rules do not write unnecessary annotations; Annotations are supplemented only when the code does not explain logic well, as part of the program, and when writing/maintaining the code while writing/maintaining the annotation; Note The Phpdocumentor specification is fully used to facilitate the generation of API-level documentation.   3.2 Detailed rules refer to the Phpdocumentor manual. Below gives a demonstration of the various parts of the annotation.   3.2.1 Copyright Information Comment Demo: 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 | +----------------------------------------------------+//      notes use//To mark copyright information, To avoid conflicts with Phpdocumentor's Page-level docblock   3.2.2 File Header Note Example     Comment name file header Comment Demo:   PHP code:   Code as follows:/** * All abstract representations of inline tags are in this file * @package phpdocumentor * @subpackage inlinetags * @since Separate file since version 1.2 * @version $Id $/*   &NBSp Note 1 The file header annotation needs to indicate the package and the sub package to which it belongs, and 2 adds $id to the @version to facilitate the use of CVS to manage the files.   3.2.3 class Annotation Sample annotation name class Comment annotation demo:    PHP code:   Code as follows:/** * Use this element to represent a {@}inline tag} like {@}link} * @see Parserstringwithinlinetags * @package phpdocumentor * @subpackage inlinetags * @author Greg Beaver &L t;cellog@users.sourceforge.net> * @since 1.0rc1 * @version $Revision: 1.21.2.6 $ * @tutorial inlinetags.pkg * *   & nbsp 3.2.4 Class attribute annotation sample annotation name class attribute annotation annotation Demo: PHP code:     Code as follows:/** * Element Type * * * type is used by many functions to skip t He hassle of * * <code> * if Get_class ($blah) = = ' Parserblah ' * </code> * always "inlinetag" * @var String * * var $type = ' Inlinetag ';     3.2.5 Function/class method annotation Sample annotation name function/class method annotation Annotation Demo: PHP code:       Code as follows:/** * @return string always ' * Calc Ulate the short description of a docblock * @see parserstringwithinlinetags::getstring () * @see parserstringwithinlinetags :: Trimmedstrlen () */function getString () { return ''; }    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.