Doc #62590 [NEW]: usort & al : documentation about callback is wrong
| From: | fabien dot delorme at gmail dot com | Date: | Tue, 17 Jul 2012 19:56:12 +0000 |
| Subject: | Doc #62590 [NEW]: usort & al : documentation about callback is wrong | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-8561@lists.php.net to get a copy of this message | ||
From: fabien dot delorme at gmail dot com
Operating system: MacOS lion
PHP version: 5.3.14
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:usort & al : documentation about callback is wrong
Description:
------------
The documentation for usort function (and, it seems, other relative
functions,
although I haven't tested all of them) is slightly wrong regarding the
callback
function. It says :
"The comparison function must return an integer less than, equal to, or
greater
than zero if the first argument is considered to be respectively less than,
equal
to, or greater than the second."
However it sometimes results in wrong results if the return value is not
precisely -1, 0 or 1. For example, returning $arg2 - $arg1 won't always
perform
the correct operation.
Even if PHP builtin comparison functions all have this behavior (returning
precisely -1, 0 or 1), this is not necessarily the case for user-defined
functions
and either code or documentation should be clear about what values shall be
returned.
Test script:
---------------
$a=array(15.44, 5.76, 10.43, 6.12, 6.17, 7.21, 8.62, 8.91);
usort($a, function($x, $y) {return $y - $x; });
var_dump($a);
// The above would work if the lambda was something like
// function($x, $y) { return $x == $y ? 0 : ($x < $y ? -1 : 1);}
Expected result:
----------------
array(8) {
[0]=>
float(15.44)
[1]=>
float(10.43)
[2]=>
float(8.91)
[3]=>
float(8.62)
[4]=>
float(7.21)
[5]=>
float(6.17)
[6]=>
float(6.12)
[7]=>
float(5.76)
}
Actual result:
--------------
array(8) {
[0]=>
float(15.44)
[1]=>
float(10.43)
[2]=>
float(8.62)
[3]=>
float(8.91)
[4]=>
float(7.21)
[5]=>
float(6.12)
[6]=>
float(5.76)
[7]=>
float(6.17)
}
--
Edit bug report at https://bugs.php.net/bug.php?id=62590&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=62590&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=62590&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=62590&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=62590&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=62590&r=needdocs
Fixed in release: https://bugs.php.net/fix.php?id=62590&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=62590&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=62590&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=62590&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=62590&r=support
Expected behavior: https://bugs.php.net/fix.php?id=62590&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=62590&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=62590&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=62590&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=62590&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=62590&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=62590&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=62590&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=62590&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=62590&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=62590&r=mysqlcfg