PHP中的!==和!=之间有区别吗?

时间:2009-07-16 17:41:12

标签: php comparison operators

PHP中的!==!=之间是否存在差异?

7 个答案:

答案 0 :(得分:28)

!=运算符会比较值,而!==运算符也会比较类型。

这意味着:

var_dump(5!="5"); // bool(false)
var_dump(5!=="5"); // bool(true), because "5" and 5 are of different types

答案 1 :(得分:8)

!===运算符的反函数,它检查各种类型之间的相等性

!=====运算符的反函数,它仅检查相同类型的事物的相等性。

答案 2 :(得分:4)

!=表示“不相等”,而!==表示“不相同”。例如:

'1' != 1   # evaluates to false, because '1' equals 1
'1' !== 1  # evaluates to true, because '1' is of a different type than 1

答案 3 :(得分:3)

!==检查类型和值,!=仅检查值

$num =  5

if ($num == "5") // true, since both contain 5
if ($num === "5") // false, since "5" is not the same type as 5, (string vs int)

答案 4 :(得分:2)

===称为身份运算符。并在其他问题的回答中详细讨论。

其他人的回答也是正确的。

答案 5 :(得分:1)

请参阅PHP type comparison tables,了解哪些值相等(==)和相同(===)。

答案 6 :(得分:1)

如果两个操作数的值不同,则运算符!=返回true。

如果运算符!==的两个操作数具有不同的值或者它们的类型不同,则它返回true。

欢呼声