(Conversion) PHP (similar to other languages) encoding standards, php Encoding
To improve work efficiency, ensure development effectiveness and rationality, and maximize the readability and reusability of program code, and improve communication efficiency, a code editing specification is required.
I. File Tag:
1. All php files use complete php labels for code marking. Short labels are not recommended (short labels are easy to be confused with xml, and php does not support short tags by default from 5.4 ).
2. For php-only code files, we recommend that you omit '?> '. This is to prevent extra spaces or other characters from affecting the code.
Ii. file and directory naming
1. the program file name and directory name are named in meaningful English. If you do not use Pinyin or meaningless letters, only letters, numbers, underscores, and hyphens are allowed '. the end of php' (except for template files). Multiple words use the hump naming method.
For example, DemoTest. class. php
// Use DemoTest. interface. php
// Other methods: demoTest. {style}. php
Iii. File directory structure
The standardized directory structure helps teams collaborate in development and post-maintenance.
-- App // Independent Application
-- Class // a single class file, a shared class file
-- Conf/inc // configuration file or directory
-- Data // data file or directory
-- Doc // program documentation
——htdocs //document_root
-- Images // path for storing all image files
-- Css // css File
-- Js // js File
-- Lib // shared class library
-- Template // template File
-- Tmp // temporary file directory
-- Cache // cache file
-- Session // SESSION file
-- Template_c // compiled Template File
——other
-- Upload // upload a file
-- Manage // manage the file directory in the background
Iv. Naming rules
1. variable name: variables in php are case-sensitive. A valid variable name starts with a number, letter, or underline, followed by any number of letters, numbers, and underscores.
A) the program name should start with lowercase letters and be meaningful. (Function displayName ())
B) PHP global variable key values have '_' on both sides and are named in the middle using the hump naming method. ($ _ GLOBAL ['_ beginTime _'])
C) Common variables use the hump naming method as a whole. We recommend that you add a prefix of the type before the variable. The uncertain type must start with an uppercase or lowercase character.
D) The function name should be meaningful and abbreviated as much as possible.
2. Class and interface naming:
A) It must start with an uppercase letter.
B) variable names composed of multiple words. There is no interval between words. Each word is capitalized.
C) the class name must be consistent with the class file name.
D) All the class names in the program are unique.
Interface naming rules:
I) use the same naming rules as the class, but add the 'I' character before the naming to indicate the interface.
Ii) Try to keep the class name consistent with the class name that implements it.
3. Database naming: All Database naming Rules do not contain uppercase letters.
A) table names all Use lowercase letters.
B) The table name uses the same prefix and the prefix cannot be blank.
C) use '_' to separate table names composed of multiple words.
D) Table field naming rules.
I) Use lowercase letters.
Ii) Multiple words are separated by underscores.
Iii) Prefix A common field with the first letter of the table name.
Iv) Avoid using keywords and reserved words.
5. annotation specifications
1. program comment: Write it in front of the code rather than behind it, and write a single line of code at the end of the Code according to your habits. For a large part of the comment, use/**/, which is usually at the top of a file or function, '//' is used inside the code. Too many comments should be used. The code comments should describe why rather than what is done, and provide the most important information to the code reader.
2. file comment: the file comment is usually placed on the top of the file, including the description, author, project name, file name, time and date, version information, and important instructions for use (class calls, note ). To modify the version.
3. Class and interface comments: according to common habits, a file contains only one class.
4. Method and function comments: the comments of methods and functions are written before. It is usually necessary to indicate the main visibility, parameter type, and return value type of information.
/**
* Connect to the database
* @ Param string $ address of the dbhost Database Server
* @ Param string $ dbuser database username
* @ Param string $ dbpwd Database Password
*/
6. Code style
1. indent and space: four spaces are used as indentation. When values are assigned to a variable, spaces are left on both sides of the equal sign. ($ Url = '$ _ GET ['url']';)
2. statement line breaking: Make sure that one line of the Program Statement is one; do not make the code of a line too long, less than 80 characters; if the code of a line is too long, use a line similar '. = 'disconnected rows; when performing SQL statement operations on the database, try not to write SQL statements in the function. Instead, use variables to define SQL statements, then, call the defined variable in the function that executes the operation.
3. Better habits: use the methods listed below in the code to make the code more elegant.
1): use the existing constants in php instead of defining them by yourself.
Example: // line feed
echo $msg."\r\n";
echo $msg,PHP_EOL;
In php, PHP_EOL is a predefined constant that indicates the end of a row. Different systems use PHP_EOL to make code more portable.
2): the efficiency of single quotes is higher than that of double quotes, but the two are different in usage. Learn to use the printf function.
Example: // echo
Echo 'every '. $ scholl.' has approximately '. floor ($ avg).' Students ';
//printf
$ Format = 'every % s is larger than $ d students ';
printf($format,$school,$avg);
3): detailed notes
4): Do not abuse syntactic sugar. Syntactic sugar is a hidden rule in the language, that is, it does not have a general representative syntax.
Case:
String, string type, add str before the variable
$strMessage = 'Hello World!' ;
Integer, an integer variable, plus 'int'
// The following variable is an integer.
$intCount = 34343;
Float, float type, plus 'fl 'in front'
// The following variable is floating point
$ FlSave = 0.8; // off
Boolean: The boolean type is preceded by 'bool'
// The following variable is boolean.
$boolEncode = true ;
Array: array type. arr is added before the variable. One-dimensional arrays Use singular nouns and multidimensional arrays use plural words.
// The following variable is a one-dimensional array.
$arrData = array ( 1 , 2 , 3 , 4 , 5 ,6) ;
// The following variable is a multi-dimensional array.
$arrMembers = array ( 'id' => 123456 , 'username' => 'ABC' , 'email' => 'abc#abc.com' ) ;
Object: object type. Add obj before the variable to indicate an object of the exact type.
$objstr = New string();
Resource, resource type. Add 'rs 'to the front'
$rsConn = mysql_connect ( 'localhost' , 'user' , 'pw' ) ;
$rsHandle = fopen( $strFilename );
Three programming naming rules (Hungary, small hump, and big hump)
1. Name of Hungary:
The START letter is the abbreviation of the variable type, and the rest is the abbreviation of the variable in English or English. The first letter of the word must be capitalized.
For example: $ Iprice = 123.0; 'I' stands For the abbreviated type.
2. Small camel: (little camel-case)
The first letter is lowercase, and the first letter is uppercase.
For example: $strPrice = 234.0;
2. big camel-case)
The first letter of each word is capitalized;
For example:$StrPrice = 324234.0;
Camel-Case is a set of naming rules for computer programming ).
Programmers can communicate with each other more easily for their own code, so they can use a uniform naming method with better readability. For example, some programmers prefer lower case letters and some programmers prefer to use underscores. Therefore, if you want to write a variable named my name, you can write myname, my_name, MyName, or myName frequently. Such naming rules are not suitable for all programmers to read. However, using the hump naming method can increase program readability.
The trigger naming method means that when a variable name or a letter name is a unique Recognition Word consisting of one or more words, the first word starts with a lowercase letter; the first letter of the second word is an uppercase letter or the first letter of each word is an uppercase letter, for example, myFirstName and myLastName. Such a variable name looks like a bucket name.
Camel-Case is a mix of upper and lower Case formats commonly used in Perl, and o'reilly, a best-selling book by Larry Wall and others, published by Programming Perl) the cover picture is exactly a camel.
The naming rules of the hump naming method can be viewed as a convention without being absolute or mandatory, in order to increase recognition and readability.