簡體   English   中英

php為什么或運算符返回0為真

[英]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

or優先級低於=優先級低於""

所以你的代碼相當於:

($a = 0) or 1;
$b = (0 || 1); 

請參閱PHP手冊中的優先級表

這是因為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;

http://php.net/manual/en/language.operators.logical.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM