【发布时间】:2012-02-20 07:18:49
【问题描述】:
在我现在正在处理的一个 PHP 项目中,我有一些类似这样的代码:
$allVarsTrue = TRUE;
if ($foo && $bar) {
for ($x=1;$x<=5;$x++) {
if (!somerandomtest($x)) {
$allVarsTrue = FALSE; // if $x fails the test, $allVarsTrue is set to false
}
}
} else { // if either $foo and $bar is false, $allVarsTrue is set to false
$allVarsTrue = FALSE;
}
if ($allVarsTrue) {
echo "True";
} else {
echo "False";
}
我想写得更简洁一些,像这样
// This code does not work.
if ($foo &&
$bar &&
for ($x=1;$x<=5;$x++) {
somerandomtest($x);
}) {
echo "True";
} else {
echo "False";
}
如何更简洁地重写现有代码?
【问题讨论】:
标签: php if-statement for-loop