Comparison Operator $a <=> $b
Spaceship Operator[php7]
When
$a
is less than, equal to, or greater than$b
, return anint
value that is less than, equal to, or greater than0
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!