if ($arg === true) { //do something here}elseif ($arg === null) { //do something else here}else { //catch all do something here}
單行條件式過長
if (($a == $b) and ($b == $c) or ($a == $e)) { $a = $d;}
if($arg === true) { //do something here}elseif($arg === null) { //do something else here}else { //catch all do something here}
條件式 if 括號旁邊請留一個空白
If, for, foreach, while, switch
<?phpfor ($i = 0; $i < $max; $i++) { //loop here}
<?phpfor ($i=0; $i<$max; $i++) { //loop here}
邏輯符號 =,< 左右兩邊請留一個空白
If, for, foreach, while, switch
<?phpforeach ($iterable as $key => $value) { // foreach body}
If, for, foreach, while, switch
<?phpwhile ($expr) { // structure body}
<?phpdo { // structure body;} while ($expr);
If, for, foreach, while, switch
switch ($var) { case 0: echo 'First case, with a break'; break; case 1: echo 'Second case, which falls through'; // no break case 2: case 3: echo 'Third case, return instead of break'; return; default: echo 'Default case'; break;}
判斷式內容請勿寫在同一行
替代 if 方案
取代if (isset($var)) { $var = 'test'; }
isset($var) and $var = 'test';
if (isset($variable)) { $variable = 'test1';}else { $variable = 'test2';}