php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #70746 Please document spaceship as returning -1 or 0 or 1
Submitted: 2015-10-19 22:19 UTC Modified: 2015-10-22 01:43 UTC
From: lbayuk at users dot sourceforge dot net Assigned: jhdxr (profile)
Status: Closed Package: Documentation problem
PHP Version: 7.0.0RC5 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: lbayuk at users dot sourceforge dot net
New email:
PHP Version: OS:

 

 [2015-10-19 22:19 UTC] lbayuk at users dot sourceforge dot net
Description:
------------
Currently the PHP manual says the PHP7 'spaceship' comparison operator <=> returns "An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b".

In the original RFC for this feature https://wiki.php.net/rfc/combined-comparison-operator the behavior was more specific: It returns -1, 0, or 1 depending on the result of the comparison.

It seems to me that the actual implementation matches the RFC: it always returns -1, 0, or -1. As coded it seems to return -1, 0, or -1 at least for all simple types such as numbers and strings. I would like to see it documented per the RFC, returning -1, 0, or 1, so we could rely on that behavior.

The orginal behavior was more useful, as it allowed for coding things like this:

    $table[($a <=> $b) + 1] 

to index a $table with 3 entries depending on the outcome of the 3-way comparison.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-19 23:16 UTC] lbayuk at users dot sourceforge dot net
Typos:
"it always returns -1, 0, or -1. As coded it seems to return -1, 0, or -1"

should of course be:
"it always returns -1, 0, or 1. As coded it seems to return -1, 0, or 1"

Sorry about that.
 [2015-10-21 15:26 UTC] jhdxr@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: jhdxr
 [2015-10-21 15:26 UTC] jhdxr@php.net
I have created a patch via edit.php.net

waiting for review.
 [2015-10-22 01:41 UTC] bwoebi@php.net
-Type: Bug +Type: Documentation Problem
 [2015-10-22 01:41 UTC] bwoebi@php.net
Automatic comment from SVN on behalf of bwoebi
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=338015
Log: Fix bug #70746 (Please document spaceship as returning -1 or 0 or 1)
 [2015-10-22 01:43 UTC] bwoebi@php.net
-Status: Verified +Status: Closed
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of bwoebi
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=08b561c7183e3a932b5109921fff41a9fbc928bf
Log: Fix bug #70746 (Please document spaceship as returning -1 or 0 or 1)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 11:01:34 2025 UTC