提问人:Lomchat 提问时间:7/16/2023 最后编辑:Lomchat 更新时间:7/17/2023 访问量:48
PHP : false OR false OR true == false?[复制]
PHP : false OR false OR true == false? [duplicate]
问:
我不明白PHP中发生了什么。 我花了几个小时,直到我明白它不能正常工作,或者我不明白什么。
这里有几行:
$test = false OR false OR true;
echo $test;
这里$test是错误的,什么都没有打印。 另外,如果我尝试:
$test = false OR false OR true;
if($test){
echo "IT'S TRUE";
}
不打印任何内容。
但是在这里:
if(false OR false OR true){
echo "IT'S TRUE";
}
打印“IT'S TRUE”。
因此,语句“false OR false OR true”在赋值给变量时为 false,但在为条件时为 true。 有谁知道并能向我解释为什么?
编辑:答案是:
$test = false OR false OR true;
被解析为
($test = false) OR false OR true;
所以我需要写:
$test = (false OR false OR true);
这是工作。 谢谢大家!
答:
3赞
Gugu72
7/16/2023
#1
$test = false OR false OR true;
实际上解析为:
($test = false) OR false OR true;
因此,为什么评估到您的病情。$test
false
详情请看这里:https://www.php.net/manual/en/language.operators.logical.php
评论
0赞
Casimir et Hippolyte
7/16/2023
另外: php.net/manual/en/language.operators.precedence.php
1赞
Lomchat
7/16/2023
好的,非常感谢!就是这么简单...
上一个:布尔逻辑和 For 循环
下一个:带有“不在乎”的基本素数隐含物
评论
=
and
or