12
votes

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

7
Very commonly duplicated question, depending on how you search for the answer: stackoverflow.com/questions/80646/…spoulson

7 Answers

32
votes

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
8
votes

!= 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.

4
votes

!= 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
3
votes

!== 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)
2
votes

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

Others' responses here are also correct.

2
votes

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

1
votes

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