What is the spaceship operator (<=>) in PHP?

PHP: What is the spaceship operator (<=>) in PHP?

The spaceship operator was a new feature in PHP 7. It looks like this: <==>

It will return 0 if both sides are equal. 1 if left is greater. -1 if right is greater.

  1.     var_dump( [
  2.  
  3.         "1 <=> 1" => 1 <=> 1,
  4.         "1 <=> 0" => 1 <=> 0,
  5.         "0 <=> 1" => 0 <=> 1,
  6.         "1 <=> \"1\"" => 1 <=> "1",
  7.         "\"abc\" <=> \"abc\"" => "abc" <=> "abc",
  8.         "\"zzz\" <=> \"abc\"" => "zzz" <=> "abc",
  9.  
  10.         ]);
array(6) {
  ["1 <=> 1"]=> 0
  ["1 <=> 0"]=> 1
  ["0 <=> 1"]=> -1
  ["1 <=> "1""]=> 0
  [""abc" <=> "abc""]=> 0
  [""zzz" <=> "abc""]=> 1
}
webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about What is the spaceship operator (<=>) in PHP?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to output (echo/print) content in PHP

How to see all of the options for an artisan command?

How to find the intersection (same values) from two arrays?

How to get the last character of a string in JS?

What are Template literals (Template strings) in JS?

How to find out what version of Laravel you are using?

What are some ways you can log debug messages and data in JS?

How to make Apache force download of files (in .htaccess)

How to replace whitespace with a single space

Set PHP config Variables (like ini_set) with .htaccess