Is there a difference between !== and != in PHP?

社会主义新天地 提交于 2019-12-17 04:34:14

问题


Is there a difference between !== and != in PHP?


回答1:


The != operator compares value, while the !== operator compares type as well.

That means this:

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



回答2:


!= is the inverse of the == operator, which checks equality across types

!== is the inverse of the === operator, which checks equality only for things of the same type.




回答3:


!= is for "not equal", while !== is for "not identical". For example:

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



回答4:


!== checks type as well as value, != only checks value

$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)



回答5:


=== is called the Identity Operator. And is discussed in length in other question's responses.

Others' responses here are also correct.




回答6:


See the PHP type comparison tables on what values are equal (==) and what identical (===).




回答7:


Operator != returns true, if its two operands have different values.

Operator !== returns true, if its two operands have different values or they are of different types.

cheers



来源:https://stackoverflow.com/questions/1139154/is-there-a-difference-between-and-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!