php.net |  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
Votes:1
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
 [2016-10-03 09:54 UTC] robin at patenall dot org dot uk
Description:
------------
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:
---------------
<?php
$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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-03 18:13 UTC] cmb@php.net
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)
	{
		$where="http://qnap/piwik/";
	}
	else
	{
		$where="https://www.someurl/piwik/";
	} 
} 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 https://bugs.php.net/bug.php?id=75980 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] cmb@php.net
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
issue.

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