PHP spaceship operator (combined comparison operators)
PHP spaceship operator (combined comparison operator)
PHP 7 newly added spaceship operator (combined comparison operator) is used to compare two expressions$a and $b, if $a is less than, equal to, or greater than $b, it returns -1, 0 or 1 respectively.
Example
<?php // 整型比较 print( 1 <=> 1);echo "<br/>"; print( 1 <=> 2);echo "<br/>"; print( 2 <=> 1);echo "<br/>"; print(PHP_EOL); // 浮点型比较 print( 1.5 <=> 1.5);echo "<br/>"; print( 1.5 <=> 2.5);echo "<br/>"; print( 2.5 <=> 1.5);echo "<br/>"; print(PHP_EOL); // 字符串比较 print( "a" <=> "a");echo "<br/>"; print( "a" <=> "b");echo "<br/>"; print( "b" <=> "a");echo "<br/>"; ?>
The above program execution output result is:
0
-1
1
0
-1
1
0
-1
1
-1
1
0
-1
1
0
-1
1