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 query mysql using PDO in PHP?

How to replace values in a string, in JS

What are Apache MPMs?

What are macros in Laravel?

How to review blog posts (written by other users) before they get published on your WordPress blog?

What is snake case?

How to merge/concat two arrays together in JS?

What is camel case?

How to block an IP address in .htaccess

How to add the CSRF (Cross-site request forgery) token in Laravel?