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 !==.
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 !==.
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
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.
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
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!');
}
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.