I am wondering how the php spaceship operator compares strings, objects and arrays. For example, the below code.
echo "Its Me at SO" <=> "Its Me at SO";
will return 0, as i know all characters are same, count is same. But if i have a code like below:
echo "Its me at SO" <=> "its Me at so";
It will return 1, means that left side is greater than right side, but how? Is it comparing the ASCII values?
Now lets come to arrays. The below code will return 0, as both arrays are equal by count, values and values at each index.
echo [1,2,3] <=> [1,2,3];
But the below code returns -1
echo [1,2,3] <=> [3,2,1];
And i dont understand why? How this operator compares the arrays and how it calculates that the array on left is smaller than the array on right? And the same goes for the objects.
Can anybody give a detailed answer that how it works with strings, arrays and objects?
Thank you
PHP will compare alpha strings using the greater than and less than comparison operators based upon alphabetical order. In the first example, ai comes before i in alphabetical order so the test of > (greater than) is false - earlier in the order is considered 'less than' rather than 'greater than'.
The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b . Comparisons are performed according to PHP's usual type comparison rules.
The spaceship operator or combined comparison operator is denoted by “<=>“. This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands. This operator has similar behavior like strcmp() or version_compare().
In PHP, variables of different data types can be compared using the loose comparison operator which is two equal signs (==). If two operands of different types are compared using loose comparison then there is an attempt to convert one or both of the operands and then compare them.
"Comparisons are performed according to PHP's usual type comparison rules (http://php.net/manual/en/types.comparisons.php)".
1) Yes, it uses the ASCII values
2) If the arrays are different lengths, the Array with fewer values is smaller.
Otherwise it compares the arrays key by key, giving "earlier" values priority. For example comparing $arr1[0]
to $arr2[0]
first. If $arr1
has a key that doesn't exist in $arr2
, the arrays aren't comparable (eg if we're using non-numeric arrays).
// Arrays are compared like this with standard comparison operators
// $arr1 and $arr2 are arrays
function standard_array_compare($arr1, $arr2)
{
// If either array has more values, that array is considered "larger"
if (count($arr1) < count($arr2)) {
return -1; // $arr1 < $arr2
} elseif (count($arr1) > count($arr2)) {
return 1; // $arr1 > $arr2
}
//Otherwise compare the array values directly
foreach ($arr1 as $key => $val) {
if (!array_key_exists($key, $arr2)) {
return null; // uncomparable, these arrays do not have the same keys
} elseif ($val < $arr2[$key]) {
return -1; // $arr1 < $arr2
} elseif ($val > $arr2[$key]) {
return 1; // $arr1 > $arr2
}
}
return 0; // $arr1 == $arr2
}
Note, the above is not PHP's actual code, just an approximate representation of the logic used.
Essentially, then, it treats an array in a similar way to comparing a big-endian number. It compares $arr1[0]
to $arr2[0]
. If they are the different it returns -1 or 1 depending which is larger. If they are the same it moves on to $arr1[1]
and $arr2[1]
. If all values are the same it returns 0 (arrays are equal)
While not exactly the same, it might be simpler to consider [1,2,3] <=> [3,2,1]
as basically equivalent to 123 <=> 321...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With