浅析php与数据库代码开发规范
1、PHP中对各类变量内容的命名规范
(1)目录命名、文件命名、局部变量命名:
使用英文名词、动词,以下划线作为单词的分隔,所有字母均使用小写
目录:upload、templates、install、manage……
文件:index.php 、 register.php 、 config.php……
变量:$user , $pay_time , $pay_del_cont ……
(2)全局常量命名:
使用英文名词、动词,所有字母都使用大写,以下划线分隔每个单词
define( 'WEBSITE_NAME', '名称' );
define( 'WEBSITE_URL', '地址' );
英文名词、动(3)数组变量命名:
词,以下划线作为分隔,所有字母均使用小写以array结束
$scope_array = array();
$book_id_array = array();
(4)对象变量命名:
英文名词、动词,以下划线作为分隔,可以完整采用类名或是简化类名,但是必须明确知道是什么类,所有字母均使用小写,在变量后加上_obj
$user_obj = new userAccount();
$pay_obj = new payOrder();
(5)类的命名:
使用英文名词,以大写字母作为词的分隔,其他的字母均使用小写,名词的首个字母使用小写,不使用下划线
class userAccount{ ……}
(6)方法命名:
使用英文名词、动词,以下划线作为单词的分隔,所有字母均使用小写
复制代码 代码如下:
class userAccount {
public $name_account=‘';
function is_account_ok(){
...
}
function add_account(){
...
}
}
关于对象属性的命名同理!
2、PHP中函数、符号、运算写作规范
(1)if语句大括号{}规则:
将大括号放置在关键词后方
使用IF语句尽量使用大括号
复制代码 代码如下:
if ( $condition ){
...
}else{
…
}
(2)、switch规则
每个case块结束处必须加上break,而default总应该存在处理未知情况,例:
复制代码 代码如下:
switch( $condition ){
case 'user':
...
break;
case 'type':
...
break;
default:
...
break;
}
(3)、声明定位规则
声明代码块需要对齐,且初次使用变量时需要初始化
$tableName = '';
$databaseObject = '';
尽量不使用以下方式,例:
$tableName;
$accuntName = '';
$databaseObject = '';
html的form表单各个元素名称与数据库字段尽量保持一致。
不要采用缺省方法测试非零值,必须显式测试,例:
if ( $name_pay_into != false ){
...
}else{
...
}
*尽可能使用单引号''而不是双引号"",在需要加入变量或是写sql语句除外。
*php文件中尽可能不出现html语句,如果实在无法解决尽量少用,考虑到模板的兼容性,
html文件中尽可能避免出现php语句。
*通常每个方法只执行一项逻辑动作事务,所以对它们的命名应该清楚的说明它们是做什么的:
用email_error_check()代替error_check()。
请注意命名不要与系统方法冲突。
3、PHP中各类注释规范
/**
* 分页预处理函数
* sql SQL语句
* page 当前页数
* limit 每页显示的数量
* maxs 查询总数
*/
function limit($sql,$page='0',$limit=10,$maxs=''){ }
//用户检测
if( $check_obj->username( $username ) == true ){ … }
$user_name = $_GET[user]; //获取用户信息
4、数据库设计与操作规范
数据库规范
数据库名称应该由概述项目内容的小写英文名词组成,以下划线分隔单词,
避免跨平台时可能出现的大小写错误。
数据表名称应该由物件对象名称的小写英文名词组成(尽可能对应系统中的业务类名称),以下划线分隔单词,避免跨平台时可能出现的大小写错误。
数据表的字段应避免使用varchar、text等不定长的类型,时间信息的字段使用int类型存储。
查询数据连接多表时各资源应该使用全名称,即tableName.fieldName,而不是fieldName。
SQL语句应尽可能符合ansi92标准,避免使用特定数据库对SQL语言的扩充特性。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
