[英]How to use OR operator inside IF in php?
我一直在嘗試在PHP中執行以下代碼:
if(!afunction() || !anotherfunction()) {code}
這些函數是預定義的,並且返回true或false,但是只有第一個條件有效,而第二個條件無效。
這是由於大多數語言都進行了常見的優化。
A || B
A || B
如果A
評估為true
則根本沒有理由評估B
,因為無論B
返回什么結果,表達式都只能是true
。 A && B
如果A
計算結果為false
則根本沒有理由對B
進行計算,因為無論B
返回什么結果,表達式都只能是false
。 如果要使兩個函數始終運行,則需要在邏輯表達式的上下文之外進行操作,例如:
$a_result = a();
$b_result = b();
if( ! $a_result || ! $b_result ) {
// code
}
$var1 = true;
$var2 = true;
if($var1 == true || $var2 == true) {
echo "Var1 or Var2 hold true value";
} // '||' is the OR operator
使用“或”即。 If($ x == 1或$ y == 2)...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.