Home >Backend Development >PHP Tutorial >Mediawiki.org PHP coding conventions, mediawiki.orgphp_PHP tutorial

Mediawiki.org PHP coding conventions, mediawiki.orgphp_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:33802browse

Mediawiki.org PHP coding conventions, mediawiki.orgphp

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

Using assignment as expression looks like a mistake

<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>

To improve code readability, Mediawiki uses a lot of spaces

Binary operator

<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>;

Follow the function name directly with parentheses; if there are parameters within the parentheses, add spaces on both sides

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

Control structure if while for foreach switch, keyword catch, followed by spaces

<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>

Forced type conversion

<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>;

Comments

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

Ternary operator

Use If unless the expression is very short. Remember it’s all about code readability.

"if" is English; ?: is not.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/909087.htmlTechArticleMediawiki.org’s PHP coding convention, mediawiki.orgphp http://www.mediawiki.org/wiki/Manual :Coding_conventions/PHP assignment as expression looks like a mistake // No if ( $a =...
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