PHP code style

巴扎黑
Release: 2016-11-24 09:58:07
Original
1629 people have browsed it

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 (), and prohibit the use of PHP end tags ("?>") for files that only contain PHP code
, use the comment "/* End of file */" 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 capitalization of abbreviations like HTTP, RFC, 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()
Copy after login

== 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 if ($user->isLoggedIn()): // checking logged in ?>
<!-- HTML goes here. -->
<?php endif; // end checking logged in ?>
<?php foreach ($users as $user): // loop users ?>
<!-- HTML goes here. -->
<?php endforeach; // end loop users ?>
...
Copy after login

== Note:

* Document block must be compatible with phpDocumentor format, please refer to: http://phpdoc.org/

* Avoid redundant comments

/**
 * 控制器类说明信息
 */
class Controller {
    private static $instance;
    public function __construct()  {
        ...
    }
    /**
     * 函数说明信息
     */
    public static function &get_instance() {
        ...
    }
/* End of file controller.php */
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!