Brief analysis of php and database code development specification

  • 2020-08-22 21:55:47
  • OfStack

1. Naming specifications for various variable contents in PHP

(1) Directory naming, file naming, local variable naming:
Use English nouns and verbs. Use underlined words to separate words. All letters are in lower case
upload, templates, install, manage...
index.php, register.php, config.php...
Variables: $user, $pay_time, $pay_del_cont...

(2) Naming of global constants:
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 and verb (3) Array variable name:
Words are separated by an underscore and all letters are in lower case ending with array
$scope_array = array();
$book_id_array = array();

(4) Naming of object variables:
English nouns and verbs are separated by an underscore. You can use the full class name or the simplified class name, but you must know exactly what class it is. All letters are in lower case, followed by _obj

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

(5) Class naming:
Use English nouns with uppercase letters as the word separators, use lowercase for all other letters, and use lowercase for the first letter of a noun, not an underscore

class userAccount {... }

(6) Method naming:
Use English nouns and verbs. Use underlined words to separate words. All letters are in lower case

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


The same goes for object properties!

2. PHP function, symbol and operation writing specification
(1) if statement curly braces {} rule:
Place curly braces after the keywords
Use the IF statement to use curly braces whenever possible
  
if ( $condition ){ 
 ... 
}else{ 
  ...  
} 

(2) switch Rules
break must be added at the end of each case block, while default should always exist to handle unknown cases, for example:
  
switch( $condition ){ 
 case 'user': 
  ... 
  break; 
 case 'type': 
  ... 
  break; 
 default: 
  ... 
  break; 
} 

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

For html's form form, keep each element name as close as possible to the database field.
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 quotation marks instead of double quotation marks whenever possible, except when you need to add variables or write sql statements.
* Avoid html statements in php files. If this is not possible, avoid using html statements as much as possible.
Avoid php statements as much as possible in the html file.
* Usually each method performs only 1 logical action transaction, so their names should clearly indicate what they do:
Replace error_check() with email_error_check().

Be careful not to clash names with system methods.

3. Various annotation specifications in PHP
/**
* Paging preprocessor function
* sql SQL statement
* page current pages
* limit number of displays per page
* 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 specifications

Database specification
The database name should consist of a lowercase English noun that summarizes the contents of the project, separated by an underscore,
Avoid case errors that can occur across platforms.

The table name should consist of a lowercase English noun of the object name (where possible corresponding to the name of the business class in the system), separated by an underscore to avoid possible capitalization errors that can occur across platforms.

Data table fields should avoid using variable length types such as varchar, text, and time information fields should be stored using int.
When querying data to join 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, avoiding the use of specific databases to augment the SQL language.


Related articles: