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

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

WBOY
WBOY原创
2016-06-13 09:21:35972浏览

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