将在今年11月发布的PHP 7将引入Spaceship(<=>)运算符。它是什么以及它如何工作?
该<=>(“飞船”),运营商将提供,它会合并比较:
<=>
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建议的这个新运算符。
<
<=
==
>=
>
//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