Home  >  Article  >  Backend Development  >  A brief analysis of PHP and database code development specifications_PHP tutorial

A brief analysis of PHP and database code development specifications_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:40737browse

1. Naming specifications for various variable contents in PHP

(1) Directory naming, file naming, and local variable naming:
Use English nouns and verbs, use underscores to separate words, and use lowercase letters for all letters
Directory: upload, templates, install, manage...
File: index.php, register.php, config.php...
Variables: $user, $pay_time, $pay_del_cont...

(2) Global constant naming:
Use English Nouns and verbs, all letters are capitalized, and each word is separated by an underscore
define( 'WEBSITE_NAME', 'name' );
define( 'WEBSITE_URL', 'address' );
English noun, Move (3) Array variable naming:
words, separated by underscores, all letters use lowercase and end with array
$scope_array = array();
$book_id_array = array();

(4) Object variable naming:
English nouns and verbs are separated by underscores. You can use full class names or simplified class names, but you must clearly know what they are. What class, all letters are in lowercase, add _obj after the variable

$user_obj = new userAccount();
$pay_obj = new payOrder();

(5) Class naming:
Use English nouns, use capital letters as word separators, use lowercase letters for other letters, use lowercase letters for the first letter of nouns, do not use underscores

class userAccount{…}

(6) Method naming:
Use English nouns and verbs, with underscores as word separators , all letters are lowercase

Copy code The code is as follows:

class userAccount {
public $name_account='' ;
function is_account_ok(){
...
}
function add_account(){
...
}
}


The same goes for naming object attributes!

2. Standards for writing functions, symbols, and operations in PHP
(1) If statement braces {} rules:
Place the braces Use IF statements after keywords
Try to use braces
Copy the code The code is as follows:

if ( $condition ){
...
}else{

}

(2), switch rule
A break must be added at the end of each case block, and default should always exist to handle unknown situations, for example:
Copy code The code is as follows:

switch( $condition ){
case 'user':
...
break;
case 'type':
...
break;
default:
...
break;
}

(3), declare positioning rules
Declare code blocks need to be aligned, and variables need to be initialized when used for the first time
$tableName = '';
$databaseObject = '';
Try not to use the following methods, for example:
$tableName;
$accuntName = '';
$databaseObject = '';

html form The names of each element of the form should be as consistent as possible with the database fields.
Do not use the default method to test non-zero values, you must test explicitly, for example:
if ( $name_pay_into != false ){
...
}else{
...
}

* Use single quotes '' instead of double quotes "" whenever possible, except when you need to add variables or write sql statements.
*As much as possible, html statements should not appear in php files. If it cannot be solved, try to use them as little as possible. Taking into account the compatibility of the template,
avoid php statements appearing in html files as much as possible.
* Usually each method only performs one logical action transaction, so their names should clearly indicate what they do:
Replace error_check() with email_error_check().

Please be careful not to name conflicts with system methods.

3. Various comment specifications in PHP
/**
* Paging preprocessing function
* sql SQL statement
* page current page number
* limit the number displayed on each page
* maxs total number of 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 specifications

Database specifications
The database name should consist of lowercase English nouns outlining the project content, with underscores separating words,
avoid crossing Possible casing errors on different platforms.

The data table name should be composed of the lowercase English noun of the object name (corresponding to the business class name in the system as much as possible), with underscores separating words to avoid possible capitalization errors when cross-platform.

The fields of the data table should avoid using variable-length types such as varchar and text. The fields of time information should be stored in int type.
When querying data and connecting multiple tables, the full name of each resource should be used, that is, tableName.fieldName, not fieldName.
SQL statements should conform to the ansi92 standard as much as possible and avoid using the extended features of the SQL language of specific databases.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328131.htmlTechArticle1. Naming specifications for various variable contents in PHP (1) Directory naming, file naming, and local variable naming : Use English nouns and verbs, use underscores as word separators, and all letters...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn