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 hide the server signature (hide Apache info) from HTTP headers with .htaccess

How to create random numbers in JS?

How to set infinite execution time in a PHP script

How to check if a user is logged in, inside a blade file?

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

What are the main error types in PHP

What are generators in PHP, and how do they compare to arrays?

PHP Date commonly used formats

How to set up an alias to map one directory to another destination

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