What is the difference between ==
and ===
?
- How exactly does the loosely
==
comparison work? - How exactly does the strict
===
comparison work?
What would be some useful examples?
What is the difference between ==
and ===
?
==
comparison work?===
comparison work? What would be some useful examples?
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
It's all about data types. Take a
BOOL
(true or false) for example:true
also equals1
andfalse
also equals0
The
==
does not care about the data types when comparing: So if you had a variable that is 1 (which could also betrue
):$var=1;
And then compare with the
==
:But
$var
does not actually equaltrue
, does it? It has the int value of1
instead, which in turn, is equal to true.With
===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.So if I did
that condition would not be true, as
$var !== true
it only== true
(if you know what I mean).Why would you need this?
Simple - let's take a look at one of PHP's functions:
array_search()
:The
array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did anarray_search()
on a value that was stored in the first element of the array (which would have the array key of0
)....thearray_search()
function would return 0...which is equal to false..So if you did:
So, do you see how this could be an issue now?
Most people don't use
== false
when checking if a function returns false. Instead, they use the!
. But actually, this is exactly the same as using==false
, so if you did:So for things like that, you would use the
===
instead, so that the data type is checked.Be careful though. Here is a notorious problem.
vs.
There are two differences between
==
and===
in PHP arrays and objects that I think didn't mention here; two arrays with different key sorts, and objects.Two arrays with different key sorts
If you have an array with a key sort and another array with a different key sort, they are strictly different (i.e. using
===
). That may cause if you key-sort an array, and try to compare the sorted array with the original one.For instance, consider an empty array. First, we try to push some new indexes to the array without any special sort. A good example would be an array with strings as keys. Now deep into an example:
Now, we have a not-sorted-keys array (e.g., 'he' came after 'you'). Consider the same array, but we sorted its keys alphabetically:
Tip: You can sort an array by key using ksort() function.
Now you have another array with a different key sort from the first one. So, we're going to compare them:
Note: It may be obvious, but comparing two different arrays using strict comparison always results
false
. However, two arbitrary arrays may be equal using===
or not.You would say: "This difference is negligible". Then I say it's a difference and should be considered and may happen anytime. As mentioned above, sorting keys in an array is a good example of that.
Objects
Keep in mind, two different objects are never strict-equal. These examples would help:
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same memory location as the object. See here.
Note: As of PHP7, anonymous classes was added. From the results, there is no difference between
new class {}
andnew stdClass()
in the tests above.Variables have a type and a value.
When you use these variables (in PHP), sometimes you don't have the good type. For example, if you do
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
This code is wrong as if
myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :because the test is that the return value "is a boolean and is false" and not "can be casted to false".