How exactly php spaceship operator compare strings, arrays and objects

走远了吗. 提交于 2020-02-28 00:32:04

问题


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


回答1:


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




回答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


来源:https://stackoverflow.com/questions/34811599/how-exactly-php-spaceship-operator-compare-strings-arrays-and-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!