Php 简明教程
PHP - Coding Standard
每家公司都基于其最佳实践遵循自己的编码规范。编码规范是必需的,因为可能会有许多开发者在研究不同的模块,因此,如果他们开始发明自己的规范,那么源代码将变得非常难以管理,并且在未来难以维护该源代码。
Every company follows its own coding standard based on its best practices. Coding standard is required because there may be many developers working on different modules so if they will start inventing their own standards then source will become very un-manageable and it will become difficult to maintain that source code in future.
以下是人们应该使用编码规范的部分原因:
Here are some reasons why one should use coding specifications −
-
Your peer programmers have to understand the code you produce. A coding standard acts as the blueprint for all the team to decipher the code.
-
Simplicity and clarity achieved by consistent coding saves you from common mistakes.
-
If you revise your code after some time then it becomes easy to understand that code.
-
Following a uniform coding standard brings more quality in software.
在使用 PHP 编码时可以遵循一些准则。
There are few guidelines which can be followed while coding in PHP.
Indenting and Line Length
使用 4 个空格的缩进,不要使用任何制表符,因为不同的计算机对制表符使用不同的设置。建议将行长度保持在大约 75-85 个字符以内,以便于更好地阅读代码。
Use an indent of 4 spaces and don’t use any tab because different computers use different setting for tab. It is recommended to keep lines at approximately 75-85 characters long for better code readability.
Control Structures
这些内容包括 if、for、while、switch 等。控制语句在控制关键字和开括号之间应该有一个空格,以区别于函数调用。强烈建议你始终使用大括号,即使在技术上它们是可选项的情况下也是如此。
These include if, for, while, switch, etc. Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. You are strongly encouraged to always use curly braces even in situations where they are technically optional.
Examples
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
default action;
}
你可以按如下方式编写 switch 语句:
You can write the switch statements as follows:
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
Function Calls
函数调用时不应该在函数名称、开括号和第一个参数之间使用空格;在逗号和每个参数之间使用空格;在最后一个参数、闭括号和分号之间不使用空格。以下是一个示例:
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here’s an example −
$var = foo($bar, $baz, $quux);
Function Definitions
函数声明遵循“BSD/Allman 样式”:
Function declarations follow the "BSD/Allman style" −
function fooFunction($arg1, $arg2 = '') {
if (condition) {
statement;
}
return $val;
}
Comments
C 风格注释 (/* */) 和标准 C++ 注释 (//) 都可以。允许使用 Perl/shell 风格注释 (#),但不鼓励这样做。
C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is allowed but discouraged.
PHP Code Tags
始终使用 <?php ?> 来分隔 PHP 代码,而不是 <? ?> 简写。这是 PHP 合规性要求,也是在不同的操作系统和设置上包含 PHP 代码的最可移植方式。
Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is required for PHP compliance and is also the most portable way to include PHP code on differing operating systems and setups.
Variable Names
-
Use all lower case letters
-
Use '_' as the word separator.
-
Global variables should be prepended with a 'g'.
-
Global constants should be all caps with '_' separators.
-
Static variables may be prepended with 's'.
Make Functions Reentrant
函数不应保留防止函数重入的静态变量。
Functions should not keep static variables that prevent a function from being reentrant.
One Statement Per Line
除了语句紧密相关之外,每行只能有一个语句。
There should be only one statement per line unless the statements are very closely related.
Short Methods or Functions
方法应限制在单个代码页中。
Methods should limit themselves to a single page of code.
在编写 PHP 程序时,应考虑很多要点。总的来说,意图应始终一致,并且仅在遵循编码标准时才有可能。如果你喜欢不同的东西,你可以设计自己的标准。
There could be many more points which should be considered while writing your PHP program. Over all intention should be to be consistent throughout of the code programming and it will be possible only when you will follow any coding standard. You can device your own standard if you like something different.