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 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:
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 they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.
PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.
Loosely checking a value would allow for some similar, but not equal, values to equate as the same:
All of these values would equate as equal using the double equal operator.
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.
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.Few of the examples
P.S.
vs.