php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69466 Spaceship operator rfc discrepancy
Submitted: 2015-04-16 08:32 UTC Modified: 2015-07-18 14:22 UTC
From: ppasindud at gmail dot com Assigned: pasindu (profile)
Status: Closed Package: *General Issues
PHP Version: master-Git-2015-04-16 (Git) OS: not need
Private report: No CVE-ID: None
 [2015-04-16 08:32 UTC] ppasindud at gmail dot com
Description:
------------
http://3v4l.org/g5mCe

Test script:
---------------
<?php


$a = (object) ["a" => "c"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 1

// only values are compared
$a = (object) ["a" => "b"]; 
$b = (object) ["b" => "b"]; 
echo $a <=> $b; // 0


Expected result:
----------------
10

Actual result:
--------------
11

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-16 11:17 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2015-04-16 11:17 UTC] cmb@php.net
It seems the RFC[1] contradicts itself:

| It is implemented by using the result of the existing internal
| compare_function that underlies the other comparison operators.

but:

| // only values are compared
| $a = (object) ["a" => "b"]; 
| $b = (object) ["b" => "b"]; 
| echo $a <=> $b; // 0

However:

    var_dump($a < $b); // => bool(false)
    var_dump($b < $a); // => bool(false)
    
And currently:

    echo $a <=> $b; // => 1
    echo $b <=> $a; // => 1
    
Apparently, there is a problem with comparing unordered values with
the spaceship operator.
    
[1] <https://wiki.php.net/rfc/combined-comparison-operator>
 [2015-04-19 15:39 UTC] levim@php.net
This ends up using `zend_hash_compare`; see http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_hash.c#zend_hash_compare

In this case it cannot find key "a" in $b so it returns 1.
 [2015-04-19 22:01 UTC] stas@php.net
Comparing such objects is not a meaningful operation, in general, so the result is not well-defined. But I don't see where the discrepancy is.
 [2015-04-22 13:47 UTC] ajf@php.net
This is just a mistake in the RFC, not in the implementation, unless this doesn't match the behaviour of <, <=, ==, >= or >.
 [2015-04-29 16:30 UTC] pasindu@php.net
Found something else today

http://3v4l.org/mO48q

echo [1, 2, 3] <=> []; // should be 1 but it gives 3
 [2015-07-18 14:22 UTC] pasindu@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: pasindu
 [2015-07-18 14:22 UTC] pasindu@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

fixed*
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 28 06:01:29 2024 UTC