views:

687

answers:

6

What is the difference between == and === in php. I am unsure when to use both.

Updated note: So that it shows up in StackOverflow search, the difference between == and === is the same as the difference between != and !==.

+45  A: 

The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.

Examples:

1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Patrick Daryll Glandien
Well, you said it better than I could have said it!
X-Istence
Nitpick: === will only return true if both operands are the same type _and the values are equal_ =)
gnud
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
Rob Stevenson-Leggett
I was thinking at exactly the same answer. +1
Leandro López
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
uuɐɯǝʃǝs
Thats a very complete answer. ;)
Arto Uusikangas
A: 

The === operator is supposed to compare exact content equality while the == operator would compare semantic equality in particular it will coerce strings to numbers.

Equality is a vast subject. See the Wikipedia article on Equality.

kmkaplan
+2  A: 

This link should tell you what you need to know.

Mike Lowen
+4  A: 

An addition to the other answers concerning object comparison:

== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b yields true.

=== compares the internal object id of the objects. Even if the members are equal, $a !== $b if they are not exactly the same object.

class TestClassA {
    public $a;
}

class TestClassB {
    public $a;
}

$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();

$a1->a = 10;
$a2->a = 10;
$b->a = 10;

$a1 == $a1;
$a1 == $a2;  // Same members
$a1 != $b;   // Different classes

$a1 === $a1;
$a1 !== $a2; // Not the same object
soulmerge
+1  A: 

As for when to use one over the other, take for example the fwrite() function in php.

This function writes content to a file stream. According to php, "fwrite() returns the number of bytes written, or FALSE on error. ". If you want to test if the function call was successful, this method is flawed.

if (!fwrite(stuff))
{
    log('error!');
}

It can return zero (and is considered successful) and your condition still gets triggered. The right way would be.

if (fwrite(stuff) === FALSE)
{
    log('error!');
}
Mario
A: 

In short, === works in the same manner that == does in most other programming languages.

PHP allows you to make comparisons that don't really make sense, example:

$y = "wauv";
$x = false;
if ($x == $y)
    ...

While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught and you will be left wondering what happened.

In PHP == compares values and performs type conversion if necessary (for instance the string "12343sdfjskfjds" will become "12343" in an integer comparison). === Will compare the value AND type and will return false if the type is not the same.

If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.

Christian P.
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
David