Re: Re: [RFC] Comparison and conversion inconsistency - need more info

From: Date: Sun, 10 Nov 2013 21:11:44 +0000
Subject: Re: Re: [RFC] Comparison and conversion inconsistency - need more info
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-70092@lists.php.net to get a copy of this message
> TRUE only evaluated as 1 otherwise FALSE. FALSE/NULL evaluated as 0 > or least. This is not helping you understand -- you're trying to create rules and fitting some behavior into them, and then withdrawing your "rules" when they appear to break. What you should be doing is using the actual stated rules of the language. I realize you're excited about correcting something that has confused you for a long time, but you are getting ahead of yourself in trying to say there's something fundamentally wrong with the way PHP works. Try to slow down and read the comparison rules. > That's what we need in document. IMO. We absolutely, positively *do not* need something like this in the docs. > However, this behavior seems weird. > [yohgaki@dev ~]$ php -r "var_dump((FALSE < -100));" > bool(true) > [yohgaki@dev ~]$ php -r "var_dump((FALSE > -100));" > bool(false) > [yohgaki@dev ~]$ php -r "var_dump((TRUE < -100));" > bool(false) > [yohgaki@dev ~]$ php -r "var_dump((TRUE > -100));" > bool(false) > It's really confusing to me :( OK, first throw out EVERYTHING you have assumed to date. Now, realize that PHP has a documented rule of comparing booleans to integers (this rule is found in the "NULL or boolean - to - anything" part of the page on comparison). The rule is: compare as booleans, where FALSE < TRUE. So take FALSE < -100 How do we compare them as booleans? • FALSE is already a bool: use FALSE • cast -100 to bool: use TRUE So the effective strict comparison is FALSE < TRUE By the definition of boolean comparison in PHP, this expression must be TRUE. How about TRUE < -100 Compare as booleans? • TRUE is a boolean: use TRUE • cast -100 to bool: use TRUE So we've got TRUE < TRUE By the definition of non-object comparison in most any language (including PHP), this expression is FALSE. -- S.

« previous php.internals (#70092) next »