The Complete Guide to PHP Spaceship Operator

PHP spaceship operator is used to compare two operands together according to 3 factors which are like the below list:

  • Less than

  • Greater than

  • Equal to

The expression of the PHP spaceship operator is written like this below syntax.

operand_1 <=> operand_1

So when you use this operator, you will compare the two operands according to less than "<", equal to "=", and greater than ">".

The spaceship operator released in PHP7

PHP Spaceship Operator Examples

The spaceship operator returns an integer value which is 0 value when the two operands have the same value.

var_dump( 10 <=> 10 ); // int(0)

If the first operand is greater than the other one it will return 1

var_dump( 100 <=> 50 ); // int(1)

If the first expression is less than the second one it will show you -1

var_dump( 5 <=> 150 ); // int(-1)

Conclusion

The spaceship operator is a new feature of PHP7, used to compare two expressions together.

It returns an integer value 0, 1, or -1

Thank you for reading

Source: Coded Tag