While trying to simulate a bit of PHP behaviour I stumbled across this:
$a=array(0 => 1, 'test' => 2);
$b=array('test' => 3, 0 => 1);
var_dump($a==$b, $a>$b, $b>$a);
According to the output from var_dump
$b
is bigger than $a
. In the PHP manual there is a Transcription of standard array comparison which states that the values of the arrays are compared one by one and if a key from the first array is missing in the second array, the arrays are uncomparable. So far so good. But if I try this (change in the first element of $a
only):
$a=array(0 => 2, 'test' => 2);
$b=array('test' => 3, 0 => 1);
var_dump($a==$b, $a>$b, $b>$a);
All three comparison results are false
. This looks like "uncomparable" to me (because the >
result is the same as the <
result, while the arrays are not ==
either, which makes no sense) but this does not fit the transcription from the PHP manual. Both keys are present in both arrays and I would expect $a
to be bigger this time because the content of key 0 is bigger in $a
(2 vs. 1).
I've tried to dig into the PHP source code and found zend_hash_compare() in zend_hash.c
, but the code there seems to work as the manual describes.
What's going on here?