I don't know in this case if you've misunderstood it, but in this case since you're not comparing two objects, the identity comparison operator will be checking that its operands are the same type before trying to compare their values. It's just that in this case both operands are already known to be strings.
That said, using ===
instead of ==
can't hurt, unless you deliberately want implicit type casting. In which case it may be better to be explicit about that and do whatever casting is necessary yourself.
For example, testing if something is false or null should usually be via ===
so that you don't get a false positive 😏 from a zero or an empty string.
Testing two objects for equality should also be via ===
; it's just a quick lookup of the object hashtable to see if the two operands have the same ID. Using ==
is a lot slower, because (a) it checks that the properties of the two objects are equal, and (b) recurses on those properties; if there is a cycle of references in there somewhere ($a->foo->bar->baz === $a
) you risk blowing the stack.
If you think you need ==
to compare two objects then what you probably really want is a custom function that checks the parts that you want to check. Maybe one of the two can provide an "Equals
" method that takes the other. Or a static method of the class that takes two arguments. Or maybe in the future PHP will recognise some sort of magic __equals
function to be invoked when ==
is used.