Home > Article > Backend Development > PHP code style
If you use a third-party framework and provide code style instructions, follow its code style standards, otherwise as follows!
== Format:
* Use UTF-8 encoding
* Use 4 spaces for indentation, tabs are prohibited
* Unix style line breaks (LF)
* Used by comma, colon and semicolon operators Spaces
* Do not use spaces before (, [ after, ],)
* Use 4 spaces for indentation in code blocks
* Use hierarchical indentation
* Use a blank line before the return value of the return method (Unless there is only one line of code) and use blank lines to distinguish between two methods
* Use blank lines to distinguish between two large logical code segments
* Keep the number of lines within 80 words, no more than 120
* Use standard PHP tag delimitation, prohibit the use of short tags (ab90e1b15fa9bd92a0def3a269726396), and prohibit the use of PHP end tags ("?>") for files that only contain PHP code
, use the comment "/* End of file 13fa444ed9b39e3119e4048c8bd4bc35 */" at the end of the file
* A single line of code must also end with a semicolon (;)
== Naming:
* The file name uses snake_case. It is forbidden to use bloated file names
* Use snake_case method for variable names, and it is forbidden to use bloated variable names
* It is forbidden to use single characters as local variables (such as $i), except in for loops
* It is forbidden to use uppercase letters as Global variables, if capital letters are used, should use SCREAMING_SNAKE_CASE
* Use CamelCase for class names, and camelCase for method names (keep the capitalization of abbreviations like HTTP, RFC, and XML)
* Use SCREAMING_SNAKE_CASE for constant names
// bad superclass.php SuperClass.php superClass.php $i = "foobar"; // 单字符变量只充许使用在for循环中 $bufferdText // 驼峰式变量,并且意思可以再精简些 $groupid // 两个单词之间需要下划线分开 $name_of_last_city_used // 太长 MyConstant // 应该用下划线并且字母没有全大写 N // 单字符 S_C_VER // 意思不清楚 class superclass class superClass function fileproperties() // 意思不清楚并且没有驼峰式命名 function fileProperties() // 意思不清楚 function getfileproperties() // 好些了,但没有驼峰式命名 // good super_class.php for ($i = 0; $i < 10; $i++) $buffer $group_id $last_city MY_CONSTANT NEWLINE SUPER_CLASS_VERSION class SuperClass function getFileProperties()
== Syntax:
For PHP code embedded in HTML, for code blocks like if, for, foreach, while, etc., use if: ... endif; for: ... endfor; foreach: ... endforeach; and while: ... endwhile;Method block
Php code
...
4e6e4f52c93f79ff5344bab6d269f1deisLoggedIn()): // checking logged in ?>
c45739fa51fbb0720840a7c2888fdcde
44bb0dd56652396d1dcb408341081cc5
Blocks must be compatible with phpDocumentor format, please refer to: http://phpdoc.org/
* Avoid redundant comments
Php code
/**
* Controller class description information
*/
class Controller {
private static $instance;
public function __construct() {
...
}
/**
*/
public static function &get_instance() {
/* End of file controller.php */
== Others:
* Keep the code simple
* Keep it consistent