简体   繁体   English

PHP 为什么我的“if”语句总是给出 TRUE?

[英]PHP Why my 'if' statement always gives TRUE?

I have been making stuff in PHP for a while, everything is always fine.我一直在用 PHP 制作东西,一切都很好。 But today I don't get this statement.但今天我没有得到这个声明。 Why is it always true?为什么总是真的?

if ($action != 1 || $action != 2) echo true; // TRUE for 0, 1, 2, 3

But the reverse logic但相反的逻辑

if ($action == 1 || $action == 2) echo true; // FALSE for 0, 3 TRUE for 1, 2

The first expression blows my mind.第一个表情让我大吃一惊。 I guess I don't understand something very very basic, not in PHP but in the Universe, so I don't get it here.我想我不明白一些非常非常基本的东西,不是在 PHP 中,而是在 Universe 中,所以我不明白。 I thought that if (FALSE || TRUE) == FALSE , but it isn't a case for second example.我认为if (FALSE || TRUE) == FALSE ,但这不是第二个例子的情况。 It works as expected.它按预期工作。

So, where is the answer how to say that: "If the variable is not 1 OR 2 - echo true".那么,如何说的答案在哪里:“如果变量不是 1 OR 2 - echo true”。 I don't understand why my if ($var != 1 OR $var != 2) echo true;我不明白为什么我的if ($var != 1 OR $var != 2) echo true; doesn't work as I expect.不像我期望的那样工作。

Negation of ($action != 1 || $action != 2) is ($action == 1 && $action == 2) . ($action != 1 || $action != 2)否定是($action == 1 && $action == 2) You can see for yourself that the latter is always false because variable can not be both 1 and 2 at the same time.你可以亲眼看到后者总是假的,因为变量不能同时为 1 和 2。 Therefore the original condition is bound to be always true.因此,原始条件必然始终为真。

It is working as it has to work.它正在工作,因为它必须工作。 See the doc http://php.net/manual/en/language.operators.logical.php请参阅文档http://php.net/manual/en/language.operators.logical.php

$a || $b $a || $b returns TRUE if either $a or $b is TRUE . $a || $b返回TRUE或者$ a或$ b为

If you try like this, hope it will make sense, see the comment on every line如果你像这样尝试,希望它会有意义,请参阅每一行的评论

$action = 0;
var_dump($action != 1 || $action != 2); //here (true || true)
$action = 1;
var_dump($action != 1 || $action != 2); //here (false || true)
$action = 2;
var_dump($action != 1 || $action != 2); //here (true || false)
$action = 3;
var_dump($action != 1 || $action != 2); //here (true || true)

Condition OR will search for first TRUE result, your code will give TRUE all the time as any value will be even not 1 or 2. From you example if $action = 1 then the condition $action != 2 will give true, also if $action = 2 then $action != 1 will give TRUE.条件 OR 将搜索第一个 TRUE 结果,您的代码将始终给出 TRUE,因为任何值甚至都不是 1 或 2。从您的示例来看,如果$action = 1那么条件$action != 2将给出真,如果$action = 2然后$action != 1将给出 TRUE。

For (If the variable is not 1 OR 2 - echo true) use this:对于(如果变量不是 1 OR 2 - echo true)使用这个:

if(!in_array($action, array(1,2)) echo "true";

EDIT: You can also check it like this:编辑:您也可以这样检查:

if(!($action == 1 || $action == 2)) echo "true";
    if ($action == 2 || $action == 1) echo true; // FALSE for 0, 3 TRUE for 1, 2

Works as intended.按预期工作。 Its basically "echo true if $action is either 1 or 2".它基本上是“如果 $action 是 1 或 2,则回显真”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM