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

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

WBOY
WBOY原創
2016-06-13 09:21:35975瀏覽

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.

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn