|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73232 Array ordering is inconsistant.
Submitted: 2016-10-03 09:54 UTC Modified: 2021-08-18 15:42 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: robin at patenall dot org dot uk Assigned:
Status: Open Package: Arrays related
PHP Version: 7.0.11 OS: Centos
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
43 - 22 = ?
Subscribe to this entry?

 [2016-10-03 09:54 UTC] robin at patenall dot org dot uk
It appears that when comparing two arrays with the same keys but in different orders it is possible for the first to be both less than and greater than the second. 

Test script:
$a = array("b" => 2, "a" => 4);
$b = array("a" => 3, "b" => 3);
/** $b = array("b" => 3, "a" => 3); This works correctly **/

if($a <  $b) echo "a is less than b\n";
if($a <= $b) echo "a is less than or equal to b\n";
if($a >  $b) echo "a is greater than b\n";
if($a >= $b) echo "a is greater than or equal to b\n";
if($a == $b) echo "a is equal to b\n";
if($a != $b) echo "a is not equal to b\n";

Expected result:
a is less than b
a is less than or equal to b
a is not equal to b

Actual result:
a is less than b
a is less than or equal to b
a is greater than b
a is greater than or equal to b
a is not equal to b


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-03 18:13 UTC]
Well, what would you expect to be printed by

  var_dump($b < $a)
 [2016-10-03 18:29 UTC] robin at patenall dot org dot uk
Based on the 'Transcription of standard array comparison' example on the php Comparison Operators page I'd expect var_dump($b < $a) to be true and var_dump($b > $a) to be false as $b['a'] < $a['a']. In the same way that var_dump($a < $b) should be true and var_dump($a > $b) should be false as $a['b'] < $b['b'].

This does make array ordering comparison not commutative, but that's what the documentation say. It also means that in no case are var_dump($a < $b) and var_dump($a > $b) both equal to true.
 [2018-02-19 12:32 UTC] rolf at rolfjentsch dot de
if (isset($_SERVER['SERVER_ADDR'])) 
	if (strpos($_SERVER['SERVER_ADDR'],"192.168.10") >= 0)
} else $where="https://someurl/piwik/";

Problem arrises when >= is processed
 [2018-02-19 12:48 UTC] spam2 at rhsoft dot net
why do you come with the same broken snippet as in on this topic? your code is simply broken because you don't understand how weak-typing works when return value of a boolean false is completly different meaning then int 0
 [2018-02-19 13:45 UTC] rolf at rolfjentsch dot de
I can only excuse my self.
I thought that a missinterpreting of >= lead o the problem. 
As you clearly stated it's not to use <? as a start tag as i did.
(which is not stated in the germen documentation)

That my posted source has a problem from improper syntax is also a benefit for me ... but it worked for years without someone complaining or leading not to the desired result
 [2018-03-18 11:42 UTC]
The problem here is that the result of the comparison depends on
which array is the "first" array: hence $a < $b and $b < $a.
However, the engine assumes that $a > $b is equivalent to $b <=
$a, which is certainly a false assumption with regard to array
comparisons.  We most certainly could fix this, and although the
results would match the documentation then, they still would not
make much sense.  Besides, the resulting BC break would be an

It seems to me this issue deserves some discussion on the
internals@ mailing list.
 [2021-08-18 15:42 UTC]
-Type: Bug +Type: Feature/Change Request
 [2021-08-18 15:42 UTC]
Re-classifying as feature request as per my comment above.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Mar 21 15:03:42 2023 UTC