[英]Difference between AND [and] &&
鑒於聲明:
if($value && array_key_exists($value, $array)) {
$hello['world'] = $value;
}
是否最好使用邏輯運算符AND而不是&&?
就他們自己而言,他們完全一樣。 所以a && b
表示與a 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;
您提供的鏈接有一個注釋:
“和”和“或”運算符的兩種不同變體的原因是它們以不同的優先級運行。 (參見操作員優先順序。)
在你的情況下,因為它是唯一的操作員,它取決於你,但它們並不完全相同 。
它們在條件語句中是相同的,但在條件賦值時要小心 :
// 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;
從您鏈接到的邏輯運算符手冊。
只有拼寫的區別..使用&&而不是AND或和的好習慣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.