不等于 PHP 中的 != 和 !==

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3641819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:31:42  来源:igfitidea点击:

Not equal to != and !== in PHP

phpoperators

提问by lawrence

I've always done this: if ($foo !== $bar)

我一直这样做: if ($foo !== $bar)

But I realized that if ($foo != $bar)is correct too.

但我意识到这if ($foo != $bar)也是正确的。

Double =still works and has always worked for me, but whenever I search PHP operators I find no information on double =, so I assume I've always have done this wrong, but it works anyway. Should I change all my !==to !=just for the sake of it?

Double=仍然有效并且一直对我有用,但是每当我搜索 PHP 运算符时,我都找不到有关 double 的信息=,所以我认为我一直都做错了,但无论如何它都有效。我应该改变我的所有!==!=只是为了它的缘故?

回答by BoltClock

==and !=do not take into account the data type of the variables you compare. So these would all return true:

==并且!=不考虑您比较的变量的数据类型。所以这些都将返回 true:

'0'   == 0
false == 0
NULL  == false

===and !==dotake into account the data type. That means comparing a string to a boolean will neverbe true because they're of different types for example. These will all return false:

===并且!==考虑到数据类型。这意味着将字符串与布尔值进行比较永远不会为真,因为例如它们的类型不同。这些都将返回 false:

'0'   === 0
false === 0
NULL  === false

You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():

您应该比较返回值可能是不明确的真/假值的函数的数据类型。一个众所周知的例子是strpos()

// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false);  // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned

回答by Sandeep

!== should match the value and data type

!== 应该匹配值和数据类型

!= just match the value ignoring the data type

!= 只匹配忽略数据类型的值

$num = '1';
$num2 = 1;

$num == $num2; // returns true    
$num === $num2; // returns false because $num is a string and $num2 is an integer

回答by Sandeep

$a !== $bTRUEif $ais not equal to $b, or they are not of the same type

$a !== $bTRUE如果$a不等于$b,或者它们的类型不同

Please Refer to http://php.net/manual/en/language.operators.comparison.php

请参阅 http://php.net/manual/en/language.operators.comparison.php

回答by Nick Craver

You can find the info here: http://www.php.net/manual/en/language.operators.comparison.php

你可以在这里找到信息:http: //www.php.net/manual/en/language.operators.comparison.php

It's scarce because it wasn't added until PHP4. What you have is fine though, if you know there may be a type difference then it's a much better comparison, since it's testing value andtype in the comparison, not just value.

它很少,因为它直到 PHP4 才被添加。不过,您所拥有的很好,如果您知道可能存在类型差异,那么这是一个更好的比较,因为它是在比较中测试值类型,而不仅仅是值。