PHP Spaceship Operator (Combined Comparison Operator)

PHP 7 新特性PHP 7 New Features

The new spaceship operator (combined comparison operator) added in PHP 7 is used to compare two expressions$aand$b, if$aless than, equal to, or greater than$bit returns -1, 0, or 1 respectively.

Example

<?php
// 整型比较
print( 1 <=> 1);print(PHP_EOL);
print( 1 <=> 2);print(PHP_EOL);
print( 2 <=> 1);print(PHP_EOL);
print(PHP_EOL); // PHP_EOL 为换行符

// 浮点型比较
print( 1.5 <=> 1.5);print(PHP_EOL);
print( 1.5 <=> 2.5);print(PHP_EOL);
print( 2.5 <=> 1.5);print(PHP_EOL);
print(PHP_EOL);

// 字符串比较
print( "a" <=> "a");print(PHP_EOL);
print( "a" <=> "b");print(PHP_EOL);
print( "b" <=> "a");print(PHP_EOL);
?>

The output of the above program execution is:

0
-1
1

0
-1
1

0
-1
1

PHP 7 新特性PHP 7 New Features

Other extensions