|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-09-13 01:11 UTC] manabu dot matsui at gmail dot com
Description: ------------ --- From manual page: http://www.php.net/language.operators.comparison --- 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. <?php $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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 18:00:01 2025 UTC |
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: <?php 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.