views:

216

answers:

7

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

+15  A: 

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
Salty
null!="null" is not false, wrong example. Also echo prints 1 for bool(true) and nothing for bool(false). The actual output of the code snippet is 11.
VolkerK
@VolkerK — I don't have a PHP interpreter in front of me, but hopefully this is a more accurate example. :-)
Ben Blank
My mistake. Thanks for the edit, Ben Blank. Hopefully the original poster didn't get confused.
Salty
+2  A: 

!== 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)
Ian Elliott
+4  A: 

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

Welbog
see also http://ca2.php.net/manual/nl/language.operators.comparison.php
txwikinger
+1  A: 

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

Others' responses here are also correct.

Matthew Vines
A: 

!= 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
lacqui
+1  A: 

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

Gumbo
A: 

Hi

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

Andriyev