Home >php教程 >php手册 >Mediawiki.org的PHP编码约定,mediawiki.orgphp

Mediawiki.org的PHP编码约定,mediawiki.orgphp

WBOY
WBOYOriginal
2016-06-13 09:21:35972browse

Mediawiki.org的PHP编码约定,mediawiki.orgphp

http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP

assignment作为expression来用看起来像个错误

<span>//</span><span> No</span>
<span>if</span> ( <span>$a</span> =<span> foo() ) {
    bar();
}</span>
<span>//</span><span> Yes</span>
<span>$a</span> =<span> foo();
</span><span>if</span> ( <span>$a</span><span> ) {
    bar();
}</span>

为提高代码可读性,Mediawiki大量使用空格

二元运算符

<span>//</span><span> No</span>
<span>$a</span>=<span>$b</span>+<span>$c</span><span>;
 
</span><span>//</span><span> Yes</span>
<span>$a</span> = <span>$b</span> + <span>$c</span>;

函数名后面直接跟括号;括号内如有参数,两边都加空格

<span>//</span><span> Yes</span>
<span>$a</span> = getFoo( <span>$b</span><span> );
</span><span>$c</span> = getBar();

控制结构 if while for foreach switch,关键字 catch,后面都有空格

<span>//</span><span> Yes</span>
<span>if</span><span> ( isFoo() ) {
    </span><span>$a</span> = 'foo'<span>;
}
 
</span><span>//</span><span> No</span>
<span>if</span><span>( isFoo() ) {
    </span><span>$a</span> = 'foo'<span>;
}</span>

强制类型转换

<span>//</span><span> Yes</span>
(int)<span>$foo</span><span>;
 
</span><span>//</span><span> No</span>
(int) <span>$bar</span><span>;
( int )</span><span>$bar</span><span>;
( int ) </span><span>$bar</span>;

注释

<span>//</span><span> Yes: Proper inline comment
//No: Missing space</span>

三元运算符

除非表达式很短,否则用 If。记住一切都为了代码可读性。

"if" is English; ?: is not.

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