PHP 7 中的 <=>(“宇宙飞船”运算符)是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30365346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is <=> (the 'Spaceship' Operator) in PHP 7?
提问by Deepak Mankotia
PHP 7, which will come out in November this year will introduce the Spaceship (<=>) operator. What is it and how does it work?
将于今年 11 月发布的 PHP 7 将引入 Spaceship (<=>) 运算符。它是什么以及它是如何工作的?
This question already has an answerin our general reference question about PHP operators.
这个问题在我们关于 PHP 运算符的一般参考问题中已经有了答案。
回答by GreenROBO
The <=>
("Spaceship") operator will offer combined comparison in that it will :
该<=>
(“飞船”),运营商将提供,它会合并比较:
Return 0 if values on either side are equal
Return 1 if the value on the left is greater
Return -1 if the value on the right is greater
The rules used by the combined comparison operator are the same as the currently used comparison operators by PHP viz. <
, <=
, ==
, >=
and >
. Those who are from Perl or Ruby programming background may already be familiar with this new operator proposed for PHP7.
组合比较运算符使用的规则与 PHP viz 当前使用的比较运算符相同。<
,<=
,==
,>=
和>
。那些来自 Perl 或 Ruby 编程背景的人可能已经熟悉这个为 PHP7 提出的新运算符。
//Comparing Integers
echo 1 <=> 1; //output 0
echo 3 <=> 4; //output -1
echo 4 <=> 3; //output 1
//String Comparison
echo "x" <=> "x"; //output 0
echo "x" <=> "y"; //output -1
echo "y" <=> "x"; //output 1
回答by Mark Amery
According to the RFC that introduced the operator, $a <=> $b
evaluates to:
根据引入 operator 的 RFC,$a <=> $b
评估为:
- 0 if
$a == $b
- -1 if
$a < $b
- 1 if
$a > $b
- 0 如果
$a == $b
- -1 如果
$a < $b
- 1 如果
$a > $b
which seems to be the case in practice in every scenario I've tried, although strictly the official docsonly offer the slightly weaker guarantee that $a <=> $b
will return
在我尝试过的每个场景中,这似乎都是实践中的情况,尽管严格的官方文档只提供稍微弱一点的保证,$a <=> $b
将返回
an integer less than, equal to, or greater than zero when
$a
is respectively less than, equal to, or greater than$b
小于、等于或大于零的整数
$a
分别小于、等于或大于$b
Regardless, why would you want such an operator? Again, the RFC addresses this - it's pretty much entirely to make it more convenient to write comparison functions for usort
(and the similar uasort
and uksort
).
无论如何,您为什么要这样的操作员?同样,RFC 解决了这个问题 - 几乎完全是为了更方便地为usort
(以及类似uasort
和uksort
)编写比较函数。
usort
takes an array to sort as its first argument, and a user-defined comparison function as its second argument. It uses that comparison function to determine which of a pair of elements from the array is greater. The comparison function needs to return:
usort
将要排序的数组作为其第一个参数,并将用户定义的比较函数作为其第二个参数。它使用该比较函数来确定数组中一对元素中的哪个更大。比较函数需要返回:
an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
如果第一个参数被认为分别小于、等于或大于第二个参数,则为小于、等于或大于零的整数。
The spaceship operator makes this succinct and convenient:
飞船操作员使这个简洁方便:
$things = [
[
'foo' => 5.5,
'bar' => 'abc'
],
[
'foo' => 7.7,
'bar' => 'xyz'
],
[
'foo' => 2.2,
'bar' => 'efg'
]
];
// Sort $things by 'foo' property, ascending
usort($things, function ($a, $b) {
return $a['foo'] <=> $b['foo'];
});
// Sort $things by 'bar' property, descending
usort($things, function ($a, $b) {
return $b['bar'] <=> $a['bar'];
});
More examples of comparison functions written using the spaceship operator can be found in the Usefulnesssection of the RFC.
更多使用 spaceship 运算符编写的比较函数示例可以在 RFC的有用部分找到。
回答by ANR Upgraded Version
Its a new operator for combined comparison. Similar to strcmp()
or version_compare() in behavior, but it can be used on all generic PHP values with the same semantics as <
, <=
, ==
, >=
, >
. It returns 0
if both operands are equal, 1
if the left is greater, and -1
if the right is greater. It uses exactly the same comparison rules as used by our existing comparison operators: <
, <=
, ==
, >=
and >
.
它是用于组合比较的新运算符。strcmp()
在行为上类似于或 version_compare(),但它可以用于所有具有相同语义的通用 PHP 值,<
, <=
, ==
, >=
, >
。它返回0
,如果两个操作数是相等的,1
如果左边为大,-1
如果有合适的就越大。它采用了完全一样使用我们现有的比较操作符相同的比较规则:<
,<=
,==
,>=
和>
。