phpcomparisonphp-7comparison-operators

How exactly does PHP's spaceship operator compare strings, arrays and objects?


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 let's 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 don't 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?


Solution

  • "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...