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

藏色散人
Release: 2023-04-10 20:40:01
forward
4169 people have browsed it

PHP Spaceship Operator

Comparison Operator $a <=> $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
Copy after login

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;
    }
});
Copy after login

After PHP7 :

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

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

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!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template