Analysis of PHP and Database code development specification _php

Source: Internet
Author: User
Keywords PHP code specification database
1. The naming specification of various variables in PHP

(1) directory naming, file naming, local variable naming:
Use English nouns, verbs, underline as the word, all letters are lowercase
Contents: Upload, templates, install, manage ...
Files: index.php, register.php, config.php ...
Variables: $user, $pay _time, $pay _del_cont ...

(2) global constant naming:
Use English nouns, verbs, all letters are capitalized, the following lines separate each word
Define (' Website_Name ', ' name ');
Define (' Website_url ', ' address ');
English noun, verb (3) array variable name:
Words, which are separated by an underscore, and all letters are terminated with a lowercase array
$scope _array = Array ();
$book _id_array = Array ();

(4) Name of the object variable:
English nouns, verbs, the following lines as a separate, can be used to complete the class name or simplify the class name, but must know exactly what class, all letters are lowercase, after the variable with _obj

$user _obj = new UserAccount ();
$pay _obj = new Payorder ();

(5) Name of class:
Use English nouns, with uppercase letters as the separation of the words, the other letters are used in lowercase, the first letter of the noun is lowercase, do not use the underscore

Class useraccount{...}

(6) method naming:
Use English nouns, verbs, underline as the word, all letters are lowercase
Copy CodeThe code is as follows:
Class UserAccount {
public $name _account= ';
function Is_account_ok () {
...
}
function Add_account () {
...
}
}


About the naming of object properties!

2. Code for functions, symbols, and arithmetic writing in PHP
(1) If statement curly braces {} rule:
Place curly braces behind keywords
Use the IF statement as much as possible with curly braces
Copy CodeThe code is as follows:
if ($condition) {
...
}else{
...
}

(2), switch rules
There must be a break at the end of each case block, and the default should always be handled in an unknown situation, for example:
Copy CodeThe code is as follows:
Switch ($condition) {
Case ' user ':
...
Break
Case ' type ':
...
Break
Default
...
Break
}

(3), declaration of positioning rules
Declares that code blocks need to be aligned, and initialization is required for the first time a variable is used
$tableName = ";
$databaseObject = ";
Try not to use the following methods, for example:
$tableName;
$accuntName = ";
$databaseObject = ";

The HTML form form forms each element name as consistent as possible with the database field.
Do not use the default method to test for non-0 values, you must explicitly test, for example:
if ($name _pay_into! = False) {
...
}else{
...
}

* Use single quotation marks instead of double quotes whenever possible, except when you need to add a variable or write a SQL statement.
*php file as far as possible without HTML statements, if it is not possible to resolve the use of less, considering the compatibility of the template,
Avoid PHP statements as much as possible in the HTML file.
* Usually each method performs only one logical action transaction, so naming them should clearly explain what they do:
Replace Error_check () with Email_error_check ().

Be aware that naming does not conflict with system methods.

3. All kinds of comments in PHP specification
/**
* Pagination preprocessing function
* SQL SQL statements
* Page Current Page
* Number of limit per page display
* Total number of MAXS queries
*/
function limit ($sql, $page = ' 0 ', $limit =10, $maxs = ') {}

User detection
if ($check _obj->username ($username) = = True) {...}

$user _name = $_get[user]; Get user Information

4. Database design and Operation specification

Database specification
The database name should consist of a lowercase English noun that outlines the contents of the project, separating the words with an underscore,
Avoid case errors that may occur when cross-platform.

The name of the data table should consist of a lowercase English noun of the object's name, which corresponds to the business class name in the system as much as possible, and the underscore separates the words, avoiding the case errors that may occur when cross-platform.

The fields of the data table should avoid using varchar, text, and other indefinite types, and the time information fields are stored using the INT type.
When querying data to concatenate multiple tables, each resource should use the full name, Tablename.fieldname instead of FieldName.
SQL statements should conform to the ANSI92 standard as much as possible and avoid using specific databases to enrich the SQL language.

  • 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.