php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27142 Inconsistent comparing
Submitted: 2004-02-04 06:54 UTC Modified: 2004-02-06 11:19 UTC
From: tomas dot matousek at matfyz dot cz Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0b3 (beta3) OS: WinXP
Private report: No CVE-ID: None
 [2004-02-04 06:54 UTC] tomas dot matousek at matfyz dot cz
Description:
------------
The way in which elements of an array are compared is inconsistent.


Reproduce code:
---------------
    $a = $c = array("101a","100","6");
    $b = $d = array("100","6","101a");

    // standard comparison:
    $f = create_function('$a,$b','return ($a<$b) ? -1:($a>$b ? +1:0);');
    
    asort($a);
    asort($b);
    uasort($c,$f);
    uasort($d,$f);
    
    print_r($a);
    print_r($b);
    print_r($c);
    print_r($d);


Expected result:
----------------
Than I would expect all 'print_r' print arrays with the same order of values. 
I would expect that items implicitly converted into numbers during comparison should precede those of type string.

Actual result:
--------------
Array
(
    [2] => 6
    [1] => 100
    [0] => 101a
)
Array
(
    [2] => 101a
    [1] => 6
    [0] => 100
)
Array
(
    [2] => 6
    [1] => 100
    [0] => 101a
)
Array
(
    [2] => 101a
    [1] => 6
    [0] => 100
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-04 19:29 UTC] sniper@php.net
See http://www.php.net/asort and notice the 2nd option for this function. Then see http://www.php.net/sort what values it accepts. Then try those values. Then fix your own compare function.

 [2004-02-05 14:30 UTC] tomas dot matousek at matfyz dot cz
Well, I can choose another sorting type by the 2nd argument.
But that doesn't solve the problem with the less-than (greater-than) operator. This operator is supposed to be transitive in common programming languages.
But in PHP it is not:

"101a" < "6" ... string comparison
"6" < "100" ... numerical comparison
"100" < "101a" ... string comparison

So I get the result "101a" < "101a" which is nonsense.
The non-transitivity obviously cause the problem with sorting which I've reported.

Maybe it is intended behavior but there is no mention about it in the documentation.
 [2004-02-05 16:28 UTC] andrey@php.net
Probably you have in mind C, C++ or Java... Any of these languages is not loosely typed. One pays for that PHP is loosely typed language somehow. For example take a look at :
http://bugs.php.net/bug.php?id=21728 . I think this (bug #27142) is related to (bug #21728)
 [2004-02-05 16:49 UTC] tomas dot matousek at matfyz dot cz
I realize differences between loosly typed languages and the others. Nevertheless, note that e.g. JavaScript is also loosely typed yet standard behavior of comparison operators.

IMHO better solution suitable for PHP is to compare numeric values of string at first. If equals than perform string comparison. This ordering is linear so no problems will occure. Moreover, the behaviour will not be much different from the current one.
The only difference I see now: 
"1zz" > "10a" using current comparison
and it will be "1zz" < "10a" using proposed one.
 [2004-02-06 11:19 UTC] sniper@php.net
This is not bug, and there's documentation bug report already filed. 

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 22:01:33 2024 UTC