if elseif
涵盖几行的陈述没有错。如果您稍后检查您的代码,或者更重要的是,如果其他人正在阅读您的代码,它会使其易于阅读、易于理解并且易于查看正在发生的事情。
请记住,编写代码总是比阅读更容易。
从文件:
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>
这真的不太可取,因为它难以阅读且容易误读。