浅析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











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.

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.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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 is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.
