Easy Tutorial
❮ Func Filesystem Fgets Func Array Column ❯

PHP Spaceship Operator (Combined Comparison Operator)

PHP 7 New Features

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

Example

<?php
// Integer comparison
print( 1 <=> 1);print(PHP_EOL);
print( 1 <=> 2);print(PHP_EOL);
print( 2 <=> 1);print(PHP_EOL);
print(PHP_EOL); // PHP_EOL is the newline character

// 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

❮ Func Filesystem Fgets Func Array Column ❯