【发布时间】:2013-04-30 01:25:07
【问题描述】:
我知道 PHP 使用惰性求值/短路运算符。但是假设我想评估一个条件中的所有表达式,例如:
$a = "Apple";
$b = "Banana";
$c = "Cherry";
function check($fruit) {
if ($fruit != "Banana") {
echo "$fruit is good.\n";
return true;
} else {
echo "$fruit is bad.\n";
return false;
}
}
if (check($a) && check($b) && check($c)) {
echo "Yummy!\n";
}
由于惰性求值,这只会输出:
Apple is good.
Banana is bad.
而不是期望的输出:
Apple is good.
Banana is bad.
Cherry is good.
这在表单验证中很有用。
所以我的问题是:有没有办法强制条件中的所有表达式在 PHP 中进行评估,如果没有,在上面的示例中获得所需结果的最佳/最快方法是什么?
【问题讨论】:
-
Yummy! 怎么样,检查 a ,b 或 c 是否也返回 true ?
-
检查 func , $var 更改为 $fruit
-
@JOELEE 错字已修复。感谢您指出这一点。
标签: php lazy-evaluation