Bug->Req #71090 [Opn]: Comparison operators should be transitive

From: Date: Fri, 29 Jul 2016 13:08:08 +0000
Subject: Bug->Req #71090 [Opn]: Comparison operators should be transitive
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202702@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71090&edit=1 ID: 71090 Updated by: cmb@php.net Reported by: reallfqq-php at yahoo dot fr -Summary: Comparison operators loop +Summary: Comparison operators should be transitive Status: Open -Type: Bug +Type: Feature/Change Request Package: Scripting Engine problem PHP Version: 7.0.0 Block user comment: N Private report: N New Comment: For reference: <https://3v4l.org/S9NjT> and <https://3v4l.org/84MV5>. > The bug report example actual shows a near-real-world use-case > where the sort function is unable to do its job. That is more > spectacular. So comparing apples to oranges is a near-real-world use-case? Anyhow, we can't simply change the historic behavior, because somebody claims it would be bug (despite the behavior is exactly documented), so I'm changing this ticket to feature request. Most likely an RFC would be required, to cater to the supposedly significant BC break. Previous Comments: ------------------------------------------------------------------------ [2015-12-11 20:20:17] danack@php.net Just to note, this is the actual output. PHP Notice: Object of class stdClass could not be converted to int in ./sorting.php on line 11 PHP Stack trace: Notice: Object of class stdClass could not be converted to int in ./sorting.php on line 11 PHP 1. {main}() ./sorting.php:0 Call Stack: PHP 2. is_sorted() ./sorting.php:23 0.0002 226856 1. {main}() ./sorting.php:0 0.0003 228360 2. is_sorted() ./sorting.php:23 bool(false) Almost as if the code is trying to tell you that you're doing something bogus... ------------------------------------------------------------------------ [2015-12-11 19:39:56] reallfqq-php at yahoo dot fr Short answer : *Wrong*. Try the following code: <?php $a = 15; $b = array(); $c = (object)array(); var_dump($a < $b); var_dump($b < $c); var_dump($c < $a); ?> Result: bool(true) bool(true) bool(true) ---------- Long answer: Please do not oversee the problem and oversimplify it. I am talking about operators, not constants. The problem is not INF at all. Have you checked the linked webpage? The first example show non-transitivity of some operators: php -r 'var_dump(TRUE == "a"); var_dump("a" == 0); var_dump(TRUE == 0);' Expected : bool(true) bool(true) bool(true) Actual: bool(true) bool(true) bool(false) Then, and I think that is the biggest concern, there is circular references in the operators chain, demonstrated in the bug report. The bug report example actual shows a near-real-world use-case where the sort function is unable to do its job. That is more spectacular. ------------------------------------------------------------------------ [2015-12-11 13:37:02] laruence@php.net the problem is INF, comparing sequence matters here. ------------------------------------------------------------------------ [2015-12-11 12:19:10] reallfqq-php at yahoo dot fr Description: ------------ PHP's comparison operators are circular, generatng situations where impossible equalities are considered valid. This bug is posted in reference to: http://phpsadness.com/sad/52 Test script: --------------- <?php function is_sorted($array) { $n = count($array); for ($a=0; $a<$n-1; $a++) { for ($b=$a+1; $b<$n; $b++) { if ($array[$a] > $array[$b]) { return false; } } } return true; } $array = array(INF, array(), (object)array()); sort($array); var_dump(is_sorted($array)) ?> Expected result: ---------------- bool(true) Actual result: -------------- bool(false) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71090&edit=1

« previous php.bugs (#202702) next »