什么是< => (PHP 7中的"Spaceship"运算符)? [英] What is <=> (the 'Spaceship' Operator) in PHP 7?
问题描述
PHP 7(将于今年11月问世)将引入Spaceship(< =>)运算符.这是什么以及它如何工作?
PHP 7, which will come out in November this year will introduce the Spaceship (<=>) operator. What is it and how does it work?
此问题已经有答案在有关PHP运算符的一般参考问题中.
This question already has an answer in our general reference question about PHP operators.
推荐答案
<=>
(太空飞船")运算符将提供组合比较,因为它将:
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
组合比较运算符使用的规则与PHP viz当前使用的比较运算符相同. <
,<=
,==
,>=
和>
.那些来自Perl或Ruby编程背景的人可能已经熟悉为PHP7提议的这个新运算符.
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.
//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
这篇关于什么是< => (PHP 7中的"Spaceship"运算符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!