Can you explain the difference between == and ===, giving some useful examples?
|
|
|||||
|
|
== compares the values of variables for equality, type casting as necessary. === checks if the two variables are of the same type AND have the same value. A full explanation of the differences are available in the PHP manual. Here's a table I put together showing how some variables compare to each other.
|
|||||||||||||||||||||
|
|
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:
Warning: two instances of the same class do NOT match the
|
|||||||||||||||||||||
|
|
In regards to Javascript The === operator works the same as the == operator but 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'
|
|||||||||
|
|
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, === compares the internal object id of the objects. Even if the members are equal,
|
|||||
|
|
It's all about data types. Take a
the
and then compare with the
But With So if i did:
That condition would not be true, as Why would you need this? Simple - Let's take a look at one of PHP's functions: the So if you did:
So, do you see how this could be an issue now? Most people don't use
So for things like that, you would use the |
||||
|
|
|
Given 1) Operator : == is "equal to". |
||||
|
|
|
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 might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes. |
|||||||||
|
|
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.
It can return zero (and is considered successful) and your condition still gets triggered. The right way would be.
|
|||||
|
Be careful though. Here is a notorious problem.
vs.
|
|||||
|
|
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
In this case strpos would return 0 which would equate to false in the test
or
which is not what you want here. |
|||
|
|
|
The Equality is a vast subject. See the Wikipedia article on Equality. |
|||||
|
|
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
because the test is that the return value "is a boolean and is false" and not "can be casted to false". |
|||||
|
protected by Shankar Damodaran Feb 22 at 9:24
Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?