Reputation: 784
There are some SO questions on this subject, but none of them answer it with an algorithm description, as it exists in JS (ECMAScript). It doesn't seems to exist in the PHP documentation.
I'm not a C developer and I could not even find the corresponding code in PHP sources. I will not sleep well anymore if I can't tell why (loose, ==
) comparing a string/number/resource to an object/array seems to always return false?
Eg. why '' == []
is false
, or why 'foo' == ['foo']
is false
.
Upvotes: 0
Views: 219
Reputation: 784
I finally found an almost satisfying answer from this blog of a security expert (Gynvael) and by reading source code. From the former, I'm only quoting the parts that answer my initial question: why (loose, ==) comparing a string/number/resource to an object/array seems to always return false? The algorithm in charge of equivalent comparison (==
) can be found here.
The main mechanics of the equality operator are implemented in the compare_function in php-src/Zend/zend_operators.c, however many cases call other functions or use big macros (which then call other functions that use even more macros), so reading this isn't too pleasant.
The operator basically works in two steps:
If both operands are of a type that the compare_function knows how to compare they are compared. This behavior includes the following pairs of types (please note the equality operator is symmetrical so comparison of A vs B is the same as B vs A): • LONG vs LONG • LONG vs DOUBLE (+ symmetrical) • DOUBLE vs DOUBLE • ARRAY vs ARRAY • NULL vs NULL • NULL vs BOOL (+ symmetrical) • NULL vs OBJECT (+ symmetrical) • BOOL vs BOOL • STRING vs STRING • and OBJECT vs OBJECT
In case the pair of types is not on the above list the compare_function tries to cast the operands to either the type of the second operand (in case of OBJECTs with cast_object handler), cast to BOOL (in case the second type is either NULL or BOOL), or cast to either LONG or DOUBLE in most other cases. After the cast the compare_function is rerun.
I think that all other cases return false.
Upvotes: 2
Reputation: 52822
There are multiple pages in the PHP documentation dedicated to loose comparison with the ==
operator. For objects, see Comparing Objects:
When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with ==), and are instances of the same class.
For loose comparison between other types, see PHP type comparison tables.
Upvotes: 3