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

From: Date: Sun, 10 Nov 2013 19:49:38 +0000
Subject: Re: [RFC] Comparison and conversion inconsistency - need more info
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-70085@lists.php.net to get a copy of this message
Hi all, On Sat, Nov 9, 2013 at 4:12 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote: > I was surprised that wired min() behavior. > The cause of this behavior is in Zend API. > > https://bugs.php.net/bug.php?id=53104 > > So I've made FRC to discuss/document/fix such behavior. > > If anyone know such behavior, please let me know so that I can write them > in the RFC. > > Thank you. > Apparently, I misunderstood the expression evaluation of NULL/FALSE of PHP for my entire PHP life :( The reason why I misunderstood the behavior is [yohgaki@dev ~]$ php -r "var_dump((TRUE < 1));" bool(false) [yohgaki@dev ~]$ php -r "var_dump((TRUE > 1));" bool(false) TRUE always evaluated as 1. I guess I've checked this behavior and made wrong assumption FALSE/NULL is always evaluated as 0, but it's not. [yohgaki@dev ~]$ php -r "var_dump((FALSE == 0));" bool(true) [yohgaki@dev ~]$ php -r "var_dump((FALSE < 0));" bool(false) [yohgaki@dev ~]$ php -r "var_dump((FALSE > 0));" bool(false) [yohgaki@dev ~]$ php -r "var_dump((NULL == 0));" bool(true) [yohgaki@dev ~]$ php -r "var_dump((NULL < 0));" bool(false) [yohgaki@dev ~]$ php -r "var_dump((NULL > 0));" bool(false) FALSE/NULL are evaluated as 0 or less(least). Then the manual could be improved. *Comparison with Various Types* Type of Operand 1Type of Operand 2Result null <http://www.php.net/manual/en/language.types.null.php> or string<http://www.php.net/manual/en/language.types.string.php> string <http://www.php.net/manual/en/language.types.string.php> Convert *NULL* to "", numerical or lexical comparison bool<http://www.php.net/manual/en/language.types.boolean.php> or null <http://www.php.net/manual/en/language.types.null.php> anythingConvert to bool <http://www.php.net/manual/en/language.types.boolean.php>, *FALSE* < *TRUE* object <http://www.php.net/manual/en/language.types.object.php> object <http://www.php.net/manual/en/language.types.object.php> Built-in classes can define its own comparison, different classes are uncomparable, same class - compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation<http://www.php.net/manual/en/language.oop5.object-comparison.php> string <http://www.php.net/manual/en/language.types.string.php>,resource<http://www.php.net/manual/en/language.types.resource.php> ornumber<http://www.php.net/manual/en/language.pseudo-types.php#language.types.number> string <http://www.php.net/manual/en/language.types.string.php>,resource<http://www.php.net/manual/en/language.types.resource.php> ornumber<http://www.php.net/manual/en/language.pseudo-types.php#language.types.number> Translate strings and resources to numbers, usual math array<http://www.php.net/manual/en/language.types.array.php> array <http://www.php.net/manual/en/language.types.array.php> Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example) object<http://www.php.net/manual/en/language.types.object.php>anything object <http://www.php.net/manual/en/language.types.object.php> is always greater array <http://www.php.net/manual/en/language.types.array.php>anything array <http://www.php.net/manual/en/language.types.array.php> is always greater http://php.net/manual/en/language.operators.comparison.php Manual states bool or null anything Convert to bool, FALSE < TRUE [yohgaki@dev ~]$ php -r "var_dump((bool)NULL);" bool(false) I really surprised by myself that I've never bitten by the misunderstanding more than 10 years. Anyway, the manual is better to explain explicitly TRUE always evaluated as 1 while NULL/FALSE is evaluated as 0 or least. Sorry for the confusion. Any comments? -- Yasuo Ohgaki yohgaki@ohgaki.net

« previous php.internals (#70085) next »