Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Spaceship Operator (Combined Comparison Operator)

PHP Spaceship Operator (Combined Comparison Operator)

PHP 7 New Features PHP 7 New Features

The spaceship operator (combined comparison operator) added in PHP 7 is used to compare two expressions $a and $b, if $a less than, equal to, or greater than $b, it returns -1, 0 or 1 respectively.

Example



 1);print(PHP_EOL);

print( 1 <=> 2);print(PHP_EOL);

print( 2 <=> 1);print(PHP_EOL);

print(PHP_EOL); // PHP_EOL 为换行符



// Floating-point comparison

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);



// String comparison

print( "a" <=> "a");print(PHP_EOL);

print( "a" <=> "b");print(PHP_EOL);

print( "b" <=> "a");print(PHP_EOL);

?>

The output of the above program is:



0

-1

1



0

-1

1



0

-1

1


PHP 7 New Features PHP 7 New Features