Difference between AND [and] &&


Difference between AND [and] &&

给定语句:

if($value && array_key_exists($value, $array)) {
         $hello['world'] = $value;
        }

使用逻辑操作符AND而不是&&是否更好?

它们本身的操作完全相同。所以a && ba and b是一样的。但是,它们并不相同,因为&&and具有更高的优先级。查看文档获取更多信息

// 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"answers"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;

从你链接到的逻辑运算符手册

只是拼写不同,最好使用&&而不是ANDAND .