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

Mediawiki.org PHP coding conventions, mediawiki.orgphp_PHP tutorial

WBOY
Release: 2016-07-13 10:14:33
Original
796 people have browsed it

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

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

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

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

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

Comments

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

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 =...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template