#27142 [Opn->Bgs]: Inconsistent comparing
| From: | sniper@php.net | Date: | Fri, 06 Feb 2004 16:19:32 +0000 |
| Subject: | #27142 [Opn->Bgs]: Inconsistent comparing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-54514@lists.php.net to get a copy of this message | ||
ID: 27142
Updated by: sniper@php.net
Reported By: tomas dot matousek at matfyz dot cz
-Status: Open
+Status: Bogus
Bug Type: Zend Engine 2 problem
Operating System: WinXP
PHP Version: 5.0.0b3 (beta3)
New Comment:
This is not bug, and there's documentation bug report already filed.
Previous Comments:
------------------------------------------------------------------------
[2004-02-05 16:49:55] 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-05 16:28:46] 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 14:30:59] 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-04 19:29:56] 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-04 06:54:13] 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
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=27142&edit=1