PHP代码风格探讨
下面的一些代码风格你更喜欢哪种呢?
1.
if (!empty($foo)) $data['foo'] = $foo; // A
if (!empty($foo)) {
$data['foo'] = $foo; // B
}
!empty($foo) && $data['foo'] = $foo; // C
2.
// A
foreach ($list as $item) {
if ($item['status'] == true) {
// ...
}
}
// B
foreach ($list as $item) {
if ($item['status'] == false) {
continue;
}
// ...
}
3.
// A
$data = [];
$data['a'] = 1;
$data['b'] = 1;
// B
$data = [
'a' => 1,
'b' => 1,
];
4.
// A
$count = 0;
while ($count < 10) {
// ...
$count++;
}
// B
$count = 0;
while (++$count < 10) {
// ...
}
5.
// A
if ($result == true)
// B
if (true == $result)
6.
// A
if ($result == 200) {
return true;
}
return false;
// B
return $result == 200 ? true : false;
推荐文章: