Home >Backend Development >PHP Tutorial >PHP coding standards

PHP coding standards

不言
不言Original
2018-03-30 15:31:191548browse

This article shares with you the coding rules for PHP. It is very practical. Friends in need can use it as a reference.

File name:

Category:
DemoTest.class.php
Interface:
DemoTest.interface.php
Others:
demoTest.{style}.php


Naming convention:

整体采用驼峰命名法,同时命名要有意义。

Global variables:
Use "_" on both sides, and use camel case naming in the middle

Constant:
                                                                                                                                                                                                                                                                             # Camel case naming method can be preceded by an abbreviation indicating the variable type. Accurate and meaningful English words must be used:

​ ​ ​ String: $sMyName

​ ​ ​ Array: $arrMyArray

Interface and class naming:
CamelCase nomenclature, starting with a capital letter.

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ together with the class file name:

For example: Class DataBase, file name: DataBase.class.php


Comments:

程序应提供必要的注释,包括文件注释,代码块注释,函数注释。

File comments:

At the head of the entire file:

​ ​ ​ ​ /**

            *
           */

Function comments:
//Place before function

Code block comment:
//Only write code comments where necessary to explain why, not what to do, and provide the most useful information.


Code style:


Indentation and spaces:

         Two spaces indent

Spaces flow out before and after the assignment character


Statement line breaks:
​ ​ Control the line length within 80 characters


Better practice:


Use PHP's existing variables instead of defining them yourself.

For line breaks, use PHP_EOL instead of \n or \r\n

Related recommendations:

How to solve php encoding conversion garbled code

Detailed explanation of PHP encoding conversion function application skills_php examples

PHP Coding Style Guide (PHP-FIG PSR-2)

The above is the detailed content of PHP coding standards. For more information, please follow other related articles on the PHP Chinese website!

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