[英]php why does or operator return 0 as true
在以下代碼中:
$a = 0 or 1;
$b = 0 || 1;
echo "$a, $b"; // 0, 1
為什么$a
等於零,我想or
和||
在PHP中是可以互換的嗎? 是什么讓or
語句返回0
?
我會假設兩個結果都是1
,使它回顯1, 1
。
這是因為PHP中的優先規則。 賦值=
運算符的優先級低於邏輯||
運算符,但優先級高於邏輯OR
運算符。 請參見此處: http : //php.net/manual/en/language.operators.precedence.php
這是因為優先順序
// 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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.