PHP - I get true on (0=='undefined') , why?
if you run this code:
if ( 0=='undefined') echo 'true'; else echo 'false';
you get 'true' which means int 0 equals the string 'undefined'.
Why开发者_Python百科 is that? what am I missing here?
This table documents that when comparing 0
and "undefined"
, both are converted to numbers first. And this section says that "undefined"
converts to the number 0
:
If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
So the results of the comparison are expected, if somewhat surprising for someone new to PHP.
If you want the comparison to return false
because the two operands are not of the same type, PHP provides the identical operator ===
that does exactly this.
精彩评论