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

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

WBOY
Release: 2016-06-13 09:21:35
Original
964 people have browsed it

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>
Copy after login
<span>//</span><span> Yes</span>
<span>$a</span> =<span> foo();
</span><span>if</span> ( <span>$a</span><span> ) {
    bar();
}</span>
Copy after login

为提高代码可读性,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>;
Copy after login

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

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

控制结构 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>
Copy after login

强制类型转换

<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>;
Copy after login

注释

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

三元运算符

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

"if" is English; ?: is not.

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template