Bug #70289 [Csd]: Different sort in php7 (and hhvm) compared to php5
| From: | rasmus@php.net | Date: | Sun, 30 Aug 2015 23:51:05 +0000 |
| Subject: | Bug #70289 [Csd]: Different sort in php7 (and hhvm) compared to php5 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195640@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70289&edit=1
ID: 70289
Updated by: rasmus@php.net
Reported by: kjarli at gmail dot com
Summary: Different sort in php7 (and hhvm) compared to php5
Status: Closed
Type: Bug
Package: Arrays related
Operating System: Ubuntu 14.04
PHP Version: 7.0.0beta3
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
nazar, actually those are the same. You are comparing integer 0 to string "is" there which
means PHP will cast "is" to int which is 0. 0==0.
You can prove this to yourself by forcing a string sort:
ksort($array, SORT_STRING);
Previous Comments:
------------------------------------------------------------------------
[2015-08-29 05:48:26] nazar at mokrynskyi dot com
Here is live example with this bug: https://3v4l.org/AErRF
Neither values nor keys are the same.
Here is example:
<?php
$array = array(
'is' => 'a',
'b'
);
ksort($array);
var_dump($array);
------------------------------------------------------------------------
[2015-08-18 11:44:44] cmb@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b5e87a579080da1452e173981b7d8a6814f15c0
Log: Fix #70289: Different sort in php7 (and hhvm) compared to php5
------------------------------------------------------------------------
[2015-08-18 11:31:08] cmb@php.net
I agree that a note in UPGRADING is appropriate, event though
the documentation (e.g. for usort()) already states:
| If two members compare as equal, their relative order in the
| sorted array is undefined.
See also the related internal discussion
<http://markmail.org/thread/trb3g4o6vqccitpd>.
------------------------------------------------------------------------
[2015-08-18 08:22:39] kjarli at gmail dot com
This report seems related: https://bugs.php.net/bug.php?id=40888
If it's not a bug and indeed related to small arrays in php7, I would rather see a small notice
in the upgrade guide than to have people "waste" time on such a trivial "issue"
as it's much better to rely on defined behavior by the developer.
------------------------------------------------------------------------
[2015-08-18 08:19:51] requinix@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves.
A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external
resources such as databases, etc. If the script requires a
database to demonstrate the issue, please make sure it creates
all necessary tables, stored procedures etc.
Please avoid embedding huge scripts into the report.
Sounds like cweiske's right. PHP's sorting has never promised that equal elements will
appear in any particular order. Also know that PHP 7 uses a different sorting algorithm for small
arrays than it does larger arrays.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=70289
--
Edit this bug report at https://bugs.php.net/bug.php?id=70289&edit=1