9

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

Altaf Hussain
  • 5,166
  • 4
  • 30
  • 47
  • 2
    It will do equality/inequality checks in the same way comparison operators do. So, it will compare strings same way it's with other operators and arrays or whatever. Spaceship is just a "wrapper" that still uses same logic underneath – Alma Do Jan 15 '16 at 13:06
  • 1
    "`echo "Its Me at SO" <=> "its Me at SO"; ` will return 0". Not for me it doesn't:it returns 1, which is what I'd expect it to, as "i" is greater than "I". Do you *really* get 0 for that? – Adam Cameron Jan 15 '16 at 13:29
  • Sorry, it was a typo error from my side. Updated my answer. – Altaf Hussain Jan 15 '16 at 13:33

3 Answers3

11

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

Jon Story
  • 2,881
  • 2
  • 25
  • 41
2

According to the new features documentation

Comparisons are performed according to PHP's usual type comparison rules.

<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
Luke
  • 3,481
  • 6
  • 39
  • 63
0

Spaceship Arrays php8

 <?php
    echo [] <=> []; // result is  0
    echo [1, 2, 3] <=> [1, 2, 3]; // result is 0
    echo [1, 2, 3] <=> []; // 1
    echo [1, 2, 3] <=> [1, 2, 1]; // 1
    echo [1, 2, 3] <=> [1, 2, 4]; // -1
    echo [1,2,3,1] <=> [1,2,4];//1 //thats mean index first piority,1st array index number getter then 2nd array so its positive answer 1
    echo [1,2,4] <=> [1,2,3,1]//-1