PHP spaceship operator (combined comparison operators)
PHP 7’s newly added spaceship operator (combined comparison operator) is used to compare two expressions $a and $b, if $a When it 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/>"; echo "<br/>"; // PHP_EOL 为换行符 // 浮点型比较 print( 1.5 <=> 1.5);echo "<br/>"; print( 1.5 <=> 2.5);echo "<br/>"; print( 2.5 <=> 1.5);echo "<br/>"; echo "<br/>"; // 字符串比较 print( "a" <=> "a");echo "<br/>"; print( "a" <=> "b");echo "<br/>"; print( "b" <=> "a");echo "<br/>"; ?>
The execution output of the above program is:
0 -1 1 0 -1 1 0 -1 1