|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76875 It is not clear what 'uncomparable' means
Submitted: 2018-09-13 01:11 UTC Modified: -
From: manabu dot matsui at gmail dot com Assigned:
Status: Open Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2018-09-13 01:11 UTC] manabu dot matsui at gmail dot com
From manual page:

In table 'Comparison with Various Types', it is said that 'if key from operand 1 is not found in operand 2 then arrays are uncomparable', and according to Example 2, the standard_array_compare function returns null in that case.

When I actually tried it, each operator returned the following value.

$x = ['a' => 1];
$y = ['b' => 2];

var_dump($x < $y);   # false
var_dump($x <= $y);  # false
var_dump($x > $y);   # false
var_dump($x >= $y);  # false
var_dump($x <=> $y); # 1

I can not explain this result from the manual description. I think it would be better to explain more clearly.

What is the meaning of 'uncomparable'? For example, the following can be considered

* If $x and $y are uncomparable, $x < $y, $x <= $y, $x > $y, $x >=$y are all false, and $x <=> $y is 1.

* It returns some kind of value, but it is indeterminate what the value is. (The above example happened to be so, in different situations it could be different results)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-13 11:26 UTC] a at b dot c dot de
"Uncomparable" means "cannot be compared". That means there is no well-ordering on arbitrary arrays that allows such comparisons to be made in any sensible fashion (how do _you_ think $x and $y should be ordered?).

['a' => 1] < ['b' => 2], for one, evaluates to FALSE because ['a' => 1] is not less than ['b' => 2]. (The operator is defined as returning TRUE if its left argument is less than its right. This is not the case here.)

In the same way, ['a' => 1] > ['b' => 2] is FALSE because ['a' => 1] is not greater than ['b' => 2] and ['a' => 1] == ['b' => 2] is FALSE because ['a' => 1] is not equal to ['b' => 2].
 [2018-09-14 00:34 UTC] manabu dot matsui at gmail dot com
I can see that there are uncomparable arrays. However, it is a problem that what kind of results can be expected (or should not be expected) when doing comparative operations on them is not clear.

For example, if it is guaranteed that $x <= $y and $x> = $y are false for uncomparable arrays $x, $y, then a function to determine whether two arrays can be compared is able to be written as follows:

function isComparable(array $x, array $y)
     return $x <= $y || $x >= $y;

If it is not guaranteed (eg like 'undefined behavior' in C or 'unspecified behavior' in Scheme), it must be implemented in a different way.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Dec 10 09:01:25 2018 UTC