Loading... > `<=>` 太空船操作符是在 PHP 7 中引入的新特性。 在 PHP 中,`<=>` 是太空船操作符(Spaceship Operator),也称为三路比较操作符。它用于比较两个表达式的值,并返回一个整数,表示这两个值的关系。 语法如下: ``` $value1 <=> $value2 ``` 工作原理如下: * 如果 `$value1` 小于 `$value2`,则返回 -1。 * 如果 `$value1` 等于 `$value2`,则返回 0。 * 如果 `$value1` 大于 `$value2`,则返回 1。 这个操作符通常在排序或比较操作中非常有用,特别是在[自定义排序函数](https://blog.csdn.net/xuaner8786/article/details/138338357)中。 示例: ``` echo 1 <=> 2; // 输出 -1,因为1小于2 echo 2 <=> 2; // 输出 0,因为2等于2 echo 3 <=> 2; // 输出 1,因为3大于2 ``` 你可以将太空船操作符与 `usort()` 等函数一起使用,以便在自定义排序数组时更方便地比较元素。 最后修改:2024 年 07 月 26 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏