How exactly php spaceship operator compare strings, arrays and objects

前端 未结 2 1184
北恋
北恋 2021-01-12 09:00

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         


        
相关标签:
2条回答
  • 2021-01-12 09:24

    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
    
    0 讨论(0)
  • 2021-01-12 09:35

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

    0 讨论(0)
提交回复
热议问题