Php 与[和]之间的差异&&;
鉴于声明:Php 与[和]之间的差异&&;,php,logical-operators,Php,Logical Operators,鉴于声明: if($value && array_key_exists($value, $array)) { $hello['world'] = $value; } 最好使用逻辑运算符,而不是单独使用&&?,它们的作用完全相同。所以a&&b的意思与a和b的意思相同。但是,它们并不相同,因为和&的优先级高于和。有关更多信息,请参阅 此示例显示了不同之处: // The result of the expression (false &
if($value && array_key_exists($value, $array)) {
$hello['world'] = $value;
}
最好使用逻辑运算符,而不是单独使用&&?,它们的作用完全相同。所以
a&&b
的意思与a和b
的意思相同。但是,它们并不相同,因为和&
的优先级高于和
。有关更多信息,请参阅
此示例显示了不同之处:
// 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;