In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what are the specifications of JoshChen_php". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the specifications of JoshChen_php.
PHP specification
1. Why the coding specification?
Coding specifications (code conventions) are particularly important to programmers for several reasons:
1. In the life cycle of a software, 80% of the money is spent on maintenance.
two。 Almost no software is maintained by the original developer throughout its life cycle.
3. Coding specifications can improve the readability of software and enable programmers to understand new code as soon as possible and thoroughly.
4. If you release the source code as a product, you need to make sure that it is well packaged and clear, just like any other product that has been built.
two。 Summary
Replace tab indentation with four spaces.
Remove "? >" at the bottom of the PHP file.
Each line of program is generally less than 80 characters, the excess part, divided into multiple lines to write.
Only one statement is written per line, and multiple phrases are not allowed to be written on one line.
Comments should be added to files and functions.
The repealed comment code should be deleted in a timely manner.
The naming of variables and functions should be standardized.
3. Editor Settin
3.1. Indent
All indents use spaces instead of Tab tabs. PHP files are indented with 4 spaces, HTML files and Javascript code embedded in HTML files are indented with 2 spaces, and individual Javascript and CSS files are indented with 4 spaces.
3.2. Character coding
All PHP and HTML files are saved as No Bom UTF-8 character encodings.
4. Code layout
4.1. Bottom of the file
Remove "? >" from the bottom of the file.
4.2. Between relatively independent program blocks, blank lines must be added after the variable description.
Example: the following example does not conform to the specification
The copy code is as follows:
If (! $valid_ni ()) {
... / / program code
}
$repssn_ind = $ssn_data ['index']-> repssn_index
$repssn_ni = $ssn_data ['index']-> ni
It should be written as follows:
If (! valid_ni () {
... / / program code
}
$repssn_ind = $ssn_data ['index]-> repssn_index
$repssn_ni = $ssn_ data [index]-> ni
4.3. Longer sentences should be written in multiple lines.
One-line program should be less than 80 characters
The longer statement should be written in multiple lines, the long expression should divide the new line at the low priority operator, the operator should be placed at the beginning of the new line, and the divided new line should be indented properly to make the typesetting neat and the statement readable.
If there is a long expression or statement in a loop, judgment, etc., it is necessary to make an adaptive partition. The long expression should divide the new line at the low priority operator, and the operator should be placed at the beginning of the new line. Example:
The copy code is as follows:
$perm_count_msg- > len = NO7_TO_STAT_PERM_COUNT_LEN
+ STAT_SIZE_PER_FRAM * strlen ($len)
$act_task_table [$frame_id * STAT_TASK_CHECK_NUMBER + $index]-> occupied
= $stat_ poi [index]-> occupied
$act_task_ table [taskno]-> duration_true_or_false
= sys_get_sccp_statistic_state ($stat_item)
If ($taskno
< $max_act_task_number) && (n7stat_stat_item_valid ($stat_item))){ ... // program code } for ($i = 0, $j = 0; ($i < $bufferKeyword['word_index']->Word_length)
& ($j)
< new_keyword->Word_length); $iTunes, $jacks +) {
... / / program code
}
4.4. Write only one statement per line
It is not allowed to write multiple phrases on one line, that is, only one statement per line. Example: the following example does not conform to the specification
$rect- > length = 0; $rect- > width = 0
It should be written as follows:
$rect- > length = 0 *. Always include curly braces
This is another situation where you are too lazy to type two more characters and cause problems for code clarity.
Example: the following example does not conform to the specification
The copy code is as follows:
If ($condition) do_stuff ()
If ($condition)
Do_stuff ()
While ($condition)
Do_stuff ()
For ($I = 0; $I
< $size; $i++) do_stuff($i); 应如下书写 复制代码 代码如下: if (condition){ do_stuff(); } while ($condition){ do_stuff(); } for ($i = 0; $i < $size; $i++){ do_stuff(); } 4.6. switch写法 示例:如下例子符合规范 复制代码 代码如下: switch (){ case '1': ..program break; case '2': ..program break; } 4.7. 大括号放在哪儿 程序块的分界符(大括号'{'和'}')应各独占一行并且位于同一列,同时与引用它们的语句左对齐。 而在函数体的开始、类的定义、以及if、for、do、while、switch、case语句中的右大括号应放在行尾, 左大括号应与右大括号所在行的行首处在同一列 示例:如下例子不符合规范 复制代码 代码如下: for (...) { ... // program code } if (...) { ... // program code } function example_fun() { ... // program code } 应如下书写:for (...){ ... // program code } if (...){ ... // program code } function example_fun(){ ... // program code } 4.8. 符号之间使用空格 采用这种松散方式编写代码的目的是使代码更加清晰。 由于留空格所产生的清晰性是相对的,所以,在已经非常清晰的语句中没有必要再留空格,如果语句已足够清晰则括号内侧(即左括号后面和右括号前面)不需要加空格,多重括号间不必加空格。在长语句中,如果需要加的空格非常多,那么应该保持整体清晰,而在局部不加空格。给操作符留空格时不要连续留两个以上空格。 示例:如下例子不符合规范 复制代码 代码如下: $i=0; if($i 8) ) ... for($i=0; $i 8)) ... for ($i = 0; $i < $size; $i++) ... $i = ($j < $size) ? 0 : 1; do_stuff($i, "foo", $b); 4.9. 字符串连接符 当使用字符串连接符时必须在句点(.)两侧加上空格。 示例:如下例子不符合规范 复制代码 代码如下: $str = ''
$str ='
'
It should be written as follows: $str ='
'
4.10. The use of blank lines
No one wants to see unordered code huddled together. When writing code, we always use some blank lines to increase the readability of the code. Reasonable use of spaces to distinguish code segments will make the logic of the code more clear. There are two situations in which we impose blank lines:
There must be one blank line before.
There must be a blank line between the two functions.
Return, die, and exit should add a blank line if there are other statements.
We do not allow extra spaces at the end of the line in the code.
5. Annotation
5.1. File header template
/ * *
* Chinese name of ShopEx online store document
* description of the class or file, html can be used here
*
* @ package
* @ version $Id$
* @ copyright 2003-2008 Shanghai ShopEx Network Tech. Co., Ltd.
* @ license Commercial
* =
, /
5.2. Function header comment
Each function should be preceded by a comment that tells a programmer what he needs to know to use this function. A minimized comment should include the meaning of each parameter, the expected input, and the output of the function. Comments should also give the behavior of this function under the error condition (and exactly what the error condition is). (comments should ensure) that others can confidently call the function in their own code without looking at the code.
In addition, it is definitely what we should do to comment on any technical, obscure, or non-obvious code. What is particularly important for documentation is any assumptions your code makes, or the premise that it works correctly. Any developer should be able to view any part of the application and determine what happened within a reasonable amount of time.
The copy code is as follows:
/ * *
* some_func
* the meaning of the function
* feel free to enter html in this part
* because this is the agreement of phpdocument.
*
Description of * @ param mixed $arg1 parameter 1
Description of * @ param mixed $arg2 parameter 2
* @ access public
* @ return bool
, /
5.3. Delete repealed comments
Abrogated comments, annotated code should be deleted in time
5.4. Constant annotated
For all variables and constants with physical meaning, if their names are not fully self-annotated, they must be annotated when declared to indicate their physical meaning. Comments for variables, constants, and macros should be placed adjacent to them or to the right.
Example:
The copy code is as follows:
/ / active statistic task number
Define ('MAX_ACT_TASK_NUMBER',1000)
Define ('MAX_ACT_TASK_NUMBER',1000); / / active statistic task number
5.5. Comment location
Comments should be similar to the code they describe, and comments on the code should be placed on the top or right of it (comments on a single statement) adjacent to it, not below, or separated from the code above it by a blank line.
Example: the following example does not conform to the specification
Example 1:
The copy code is as follows:
/ / get replicate sub system index and net indicator
$repssn_ind = $ssn_data [$index]-> repssn_index
$repssn_ni = $ssn_data [$index]-> ni
Example 2:$repssn_ind = $ssn_data [$index]-> repssn_index
$repssn_ni = $ssn_data [$index]-> ni
/ / get replicate sub system index and net indicator
It should be written as follows
/ / get replicate sub system index and net indicator
$repssn_ind = $ssn_data [$index]-> repssn_index
$repssn_ni = $ssn_data [$index]-> ni
5.6. Data structure declaration with comments
The data structure declaration (array) must be commented. Comments on the data structure should be placed adjacent to it, not below; notes to each field in the structure are released to the right of the field.
Example: described in the following form
The copy code is as follows:
/ / sccp interface with sccp user primitive message name
$sccp_user_primitive = array (
'NumberUNITDATANGIND' = > 1, / / sccp notify sccp user unit data come
'N_NOTICE_IND = > 2, / / sccp notify user the No.7 network can not transmission this message
N_UNITDATA_REQ = > 3 / / sccp user's unit data transmission request
)
5.7. Global variable comment
Global variables should be annotated in detail, including descriptions of their functions, value ranges, which functions or procedures access it, and considerations when accessing it.
5.8. Annotation indentation
Comments are indented in the same way as the contents described, which can make the program neatly typeset and facilitate the reading and understanding of notes.
Example: the following example does not conform to the specification
The copy code is as follows:
Function example_fun () {
/ / code one comments
CodeBlock One
/ / code two comments
CodeBlock Two
}
It should be changed to the following layout:
The copy code is as follows:
Function example_fun () {
/ / fdgfd
CodeBlock One
/ / code two comments
CodeBlock Two
}
5.9. Separate the comment from the code above it with a blank line
Example: the following example shows that the code is too compact.
The copy code is as follows:
/ / code one comments
Program code one
/ / code two comments
Program code two
/ / code one comments should be written as follows
Program code one
/ / code two comments
Program code two
5.10. Consecutive case comments
For the case statement under the switch statement, if you need to finish processing one case and proceed to the next case processing due to special circumstances, you must add explicit comments before the next case statement after the case statement has finished processing. In this way, the intention of the programmer is clearer, and the break statement is effectively prevented from being left out without reason.
Example:
The copy code is as follows:
Switch ($I) {
Case 'CMD_INIT':
Echo "I equals 0"
Break
Case 'CMD_START:
Echo "I equals 1"; / / now jump into case CMD_A
Case 'CMB_A':
Echo "I equals 2"
Break
}
5.11. Structure declaration
The array variables that represent the structure in the code should be declared in advance.
Example:
The copy code is as follows:
Function example_fun () {
$student = array (
'name' = >' Xiaoming', / / name
'addr' = >' detailed address', / / address
'sex' = >' male', / / gender
'city' = >' Shanghai'/ / City
)
}
5.12. Comment format
The format of comments is uniform, single-line comments must use "/ /...", multiple lines use a pair of / *. , /
Example: the following example does not conform to the specification.
The copy code is as follows:
/ * if receive_flag is TRUE * /
/ * if receive_flag is FALSE * /
If ($receive_flag)
It should be written as follows:
The copy code is as follows:
/ * if receive_flag is TRUE
If receive_flag is FALSE * /
If ($receive_flag)
5.13. The notes are mainly in Chinese.
The notes should take into account the readability of the program and the appearance of typesetting. if the language used is both Chinese and English, it is recommended to use Chinese unless it can be expressed in very fluent and accurate English.
6. Naming regulation
6.1. Prohibition of pinyin nomenclature
Pinyin nomenclature is prohibited in the code.
6.2. Variable naming
Variable names should be all lowercase and words should be separated by a single underscore.
For example, $current_user is correct, but $currentuser and $currentUser are not.
The name should be descriptive and concise. We naturally don't want to use lengthy sentences as variable names, but it's better to enter a few more characters than to wonder what a variable is for.
6.3. Function naming
Use lowercase names separated by a single underscore between words, allowing verb-object phrases to name functions that perform an operation. If it is an OOP method, there can be only verbs (nouns are objects themselves). Allows the naming of system table functions.
Example:
The copy code is as follows:
Function print_record ($rec_ind)
Function input_record ()
Function get_current_color ()
Function is_boy ()
Verb list: add / edit / remove begin / end create / destroy
First / last get / release get / set
Increment / decrement put / get
Lock / unlock open / close
Min / max old / new start / stop
Next / previous source / target show / hide
Send / receive
Cut / paste up / down
Thesaurus: is has
For private methods, start with _.
6.4. Cycle counter
The only case in which a single-character variable name is allowed is when it is used as a loop counter. In this case, the counter for the outer loop should always be $I. If there is a loop inside the loop, its counter should be $j, then $k, and so on. This specification does not apply if the counter of the loop is a variable that already exists and has a meaningful name.
For example:
The copy code is as follows:
For ($I = 0; $I
< $outer_size; $i++){ for ($j = 0; $j < $inner_size; $j++){ foo($i, $j); } } 6.5. 函数参数 参数遵循和变量名字相同的约定。我们不希望一堆这样的函数:do_stuff($a, $b, $c)。在大部分情况下,我们希望仅仅看看函数的声明,就知道怎样使用它。 7. 可读性 7.1. 运算符的优先级 注意运算符的优先级,并用括号明确表达式的操作顺序,避免使用默认优先级。防止阅读程序时产生误解,防止因默认的优先级与设计思想不符而导致程序出错。 示例:下列语句中的表达式 复制代码 代码如下: $word = ($high trunk_state = TRUNK_BUSY; ... // program code } 7.3. 源程序中关系较为紧密的代码应尽可能相邻 便于程序阅读和查找。 示例:以下代码布局不太合理。 复制代码 代码如下: $rect->Length = 10
$char_poi = $str
$rect- > width = 5
It may be clearer if you write in the following form.
The copy code is as follows:
$rect- > length = 10
$rect- > width = 5; / / the length and width of the rectangle are closely related and put together.
$char_poi = $str
8. Function
8.1. Validity check of interface function parameters
The validity check of the function parameters should be the responsibility of the caller of the function, and the interface function does the necessity and legitimacy check (not mandatory).
It is summarized as follows: the outside is the main, the inside is the auxiliary, and the interior is not compulsory.
8.2. Function scale
The size of the function is limited to 100 lines, excluding comments and space lines.
8.3. A function completes only one function.
8.4. Don't design multi-purpose functions.
In addition to scheduling functions, multi-functional functions are likely to make it difficult to understand, test and maintain functions.
8.5. Multiple pieces of code repeat the same thing.
If multiple pieces of code do the same thing over and over again, there may be a problem with the partition of the function. If there is a substantial relationship between the statements of this code and completes the same function, then consider constructing this code into a new function.
9. qa
9.1. Compatibility
9.2. Ternary operator
Ternary operator, only one level is allowed in a single line of code
Ternary operators should only be used to do simple things. They are only suitable for assignment, not for function calls or anything complicated at all. If used improperly, they can affect readability, so don't indulge in using them to reduce typing.
Example: where they should not be used
($I
< $size) && ($j >$size)? Do_stuff ($foo): do_stuff ($bar)
Example: appropriate place to use them $min = ($I
< $j) ? $i : $j; 9.3. 初始化变量 变量使用前应初始化,error_reporting 将加入 E_NOTICE。意味着,变量未初始化将报错。这个问题最容易在检查 HTML 表单传递了什么变量时出现。这些错误可以通过使用内嵌的 isset() 或者empty()函数检查一个变量是否被设置来避免。 示例: 老办法 if ($forum) ... 新办法:if (!empty($forum)) ... if (isset($forum)) … 9.4. 引用字符串 在 PHP 中有两种不同的方式引用字符串--使用单引号或使用双引号。主要区别是:解析器在双引号括起的字符串中执行变量替换,却不在单引号括起的字符串中执行。因此,应当始终使用单引号,除非你确实需要对字符串进行变量替换。这样,我们可以避免让解析器解析一堆不需要执行替换的字符串的麻烦。同样,如果你使用字符串变量作为函数调用的一部分,你不需要用引号把那个变量括起来。同样,那只会给解析器增加不必要的工作。无论如何,要注意几乎所有双引号中的转义序列在单引号中都不会起作用。如果这条规范使你的代码难以阅读的话,要小心,并且放心地打破它。 示例:如下例子不符合规范 复制代码 代码如下: $str = "This is a really long string with no variables for the parser to find."; do_stuff("$str"); 应如下书写: 复制代码 代码如下: $str = 'This is a really long string with no variables for the parser to find.'; do_stuff($str); 当由于可读性的原因不得不使用双引号作为引用符时,注意其中所有的变量需用{}包围:$str = " This is '{$what}' with no variables for the parser to find." 9.5. 关联数组的键名 在 PHP 中,使用一个不用引号括起来的字符串作为一个关联数组的键名是可以运行的。我们不想这样做--为了避免混乱,这个字符串应当用引号括起来。注意,这只是当我们使用字符串时的情况,不是当我们使用变量时的情况。示例:如下例子不符合规范 $foo = $assoc_array[blah]; 应如下书写: $foo = $assoc_array['blah']; 9.6. 简化运算符 简化自增($i++)和自减($i--)运算符是导致可读性问题的仅有的简化运算符。这些运算符不应当被用作表达式的一部分。然而,他们可以独占一行使用。在表达式中使用它们(带来的便利)还不够调试时头痛的(代价)。 示例:如下例子不符合规范 复制代码 代码如下: $array[++$i] = $j; $array[$i++] = $k; 应如下书写:$i++; $array[$i] = $j; $array[$i] = $k; $i++; 9.7. if 以及else if的写法 当条件语句中的条件存在多个,并且有变量值的判断的时候,需要把变量的判断语句放在其他的条件语句之前。 示例:如下例子不符合规范 复制代码 代码如下: if (function_exists('ob_gzhandler') && $val == 1){ } 应如下书写:if ($val == 1 && function_exists('ob_gzhandler')){ } 虽然在 PHP 中else if 和 elseif 的作用基本上是一样的。但是为了代码的统一性(也有传言 else if 会出现不稳定的情况),我们要求将 elseif 之间不保留空格:if ($bool == 2){ }elseif ($n = 1){ } 9.8. 输入变量的初始化 无论是函数的参数还是通过URL传递的变量,在调用之前均必须对其进行预处理以及设定默认值。 字符串必须进行trim及转义的处理,并且如果变量的值是在我们预计的范围之内,需要对变量的非法值做出相应的处理;对于数字型的变量则需要进行intval或者floatval的处理。 9.9. require和include 在程序中需要使用包含文件的时候我们要求使用require_once或者include_once,不允许使用require或者include。 对于程序必须包含的文件只能采用require_once,而对于某些有条件包含的文件在引用时只能使用include_once。 9.10. 文件命名 文件名应当全部小写,并且词语之间以单个下划线分隔。 例如: current_user.php 是正确的, 但是 currentuser.php 和 currentUser.php 就不正确。 名称应当是描述性的,并且简明。我们自然不希望使用冗长的句子作为文件名,但是多输入几个字符总好于疑惑于某个文件到底是干什么用的。 10. SQL语法 10.1. SQL 代码布局 既然我们都在使用不同的编辑器设置,不要尝试去做诸如在 SQL 代码中实现列对齐此类的麻烦事。要做的是,不管用何种方法,把语句断行到它们单独的行上去。这里有一个 SQL 代码看上去应该是什么样子的示例。注意在哪里断行,大写,和括号的用法。 例如: 复制代码 代码如下: SELECT field1 AS something, field2, field3 FROM `table` a, `table` b WHERE (this = that) AND (this2 = that2) 10.2. 表名和字段值 SQL语句中的表名与字段名避免使用保留字;同时所有字段值的变量名,如果是数值型,需要强制类型转换。intval,floatval… 10.3. SQL select 语句 在已知需要查询的字段的前提下,不允许使用如下的代码: SELECT * FROM `mytable` 取而代之的写法是将每一个字段名写上去,请不要偷懒。SELECT col1, col2, col3 FROM `mytable` 在需要获得已知记录数量情况下,请使用 LIMIT offset, count 的方式,尽量不要使用无 LIMIT 的 SELECT 语句。 在需要或者满足条件的记录数量的情况下,请使用 SELECT count([*|col1]) FROM 的方式,尽量不要使用 SELECT col1 FROM 的方式。 需要进行逻辑运算的时候,尽量不要使用不等于,可以使用大于或者小于的方式。 10.4. SQL insert 语句 SQL INSERT 语句可以写成两种不同方式。或者你明确指明要插入的列,或者你已经知道数据中各列的顺序,不用详细指定它们。我们希望使用前一种方法,也就是详细说明插入哪些列。这意味着应用程序代码不会依赖于数据库中字段的顺序,也不会因为我们增加另外的字段而崩溃(当然,除非它们被指定为 NOT NULL)。 例如: # 这不是我们想要的 复制代码 代码如下: INSERT INTO `mytable` VALUES ('something', 1, 'else') # 这是正确的。 复制代码 代码如下: INSERT INTO `mytable` (column1, column2, column3) VALUES ('something', 1, 'else') 11. smarty语法 11.1. 界定符 界定符为 11.2. 双引号、单引号 为了避免dreamweaver将Smarty语句中的双引号改写为",我们要求在Smarty的花括号中不允许使用双引号,而是使用单引号。 错误的写法: 匿名用户 正确的写法: 匿名用户 11.3. 有条件设置HTML属性值 当需要在模板中有条件的设置HTML元素属性值的时候,我们要求所有语句均包含在双引号之内。错误的代码: 复制代码 代码如下: "promote_goods" "normal_goods" >The correct way to write:
The copy code is as follows:
11.4. Conditional modifier
In smarty, you can use eq, neq, gt, lt, and so on to represent = =,! =, >, and so on.
At this point, I believe you have a deeper understanding of "what are the specifications of JoshChen_php?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.