【发布时间】:2011-08-19 16:23:47
【问题描述】:
鉴于声明:
if($value && array_key_exists($value, $array)) {
$hello['world'] = $value;
}
最好使用逻辑运算符AND 而不是&&?
【问题讨论】:
-
它们的功能相同,所以没关系。我的建议就是选择一个并坚持下去。
标签: php logical-operators
鉴于声明:
if($value && array_key_exists($value, $array)) {
$hello['world'] = $value;
}
最好使用逻辑运算符AND 而不是&&?
【问题讨论】:
标签: php logical-operators
就他们自己而言,他们做的完全一样。所以a && b 的含义与a and b 相同。但是,它们并不相同,因为&& 的优先级高于and。请参阅the docs 了解更多信息。
这里的这个例子显示了不同之处:
// The result of the expression (false && true) is assigned to $e
// Acts like: ($e = (false && true))
$e = false && true;
// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) and true)
$f = false and true;
【讨论】:
您提供的链接有注释:
“and”和“or”运算符的两种不同变体的原因是它们以不同的优先级操作。 (请参阅运算符优先级。)
在您的情况下,由于它是唯一的运算符,这取决于您,但它们完全相同。
【讨论】:
它们在条件语句中是相同的,但要小心条件赋值:
// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;
// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;
和
// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;
// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;
来自您链接到的 Logical Operators 手册。
【讨论】:
只有拼写的区别..使用 && 代替 AND 或 and..
【讨论】: