Home >Backend Development >PHP Tutorial >Let's talk about the usage scenarios of PHP spaceship operator

Let's talk about the usage scenarios of PHP spaceship operator

藏色散人
藏色散人forward
2021-12-21 16:29:224259browse

PHP Spaceship Operator

Comparison Operator $a 96b4fef55684b9312718d5de63fb7121 $b Spaceship Operator[php7]

When $a is less than, equal to, or greater than $b, return an int value that is less than, equal to, or greater than 0 respectively.

// 示例
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Usage scenarios

Before PHP7:

$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
    if ($a < $b) {
        return -1;
    } elseif ($a > $b) {
        return 1;
    } else {
        return 0;
    }
});

After PHP7 :

$arr = [4,2,1,3];

usort($arr, function ($a, $b) {
    return $a <=> $b;
});

If you have more usage scenarios, you can leave a comment.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Let's talk about the usage scenarios of PHP spaceship operator. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete