PHP头条
热点:

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


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

assignment作为expression来用看起来像个错误

// No
if ( $a = foo() ) {
    bar();
}
// Yes
$a = foo();
if ( $a ) {
    bar();
}

为提高代码可读性,Mediawiki大量使用空格

二元运算符

// No
$a=$b+$c;
 
// Yes
$a = $b + $c;

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

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

控制结构 if while for foreach switch,关键字 catch,后面都有空格

// Yes
if ( isFoo() ) {
    $a = 'foo';
}
 
// No
if( isFoo() ) {
    $a = 'foo';
}

强制类型转换

// Yes
(int)$foo;
 
// No
(int) $bar;
( int )$bar;
( int ) $bar;

注释

// Yes: Proper inline comment
//No: Missing space

三元运算符

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

"if" is English; ?: is not.

www.phpzy.comtrue/php/29264.htmlTechArticleMediawiki.org的PHP编码约定,mediawiki.orgphp http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP assignment作为expression来用看起来像个错误 // No if ( $a = foo() ) { bar();} // Yes $a = foo(); if ( $a ) { bar();} 为提...

相关文章

PHP之友评论

今天推荐