PHP7 <=> 'Spaceship' Operator Explanation, Questions & Answers

PHP7, which will come out in September this year will introduce a new type of operator, called 'SpaceShip' operator. This <=> operator will offer combined comparison in that it will -
  • Return 0 if values on either side are equal
  • Return 1 if value on the left is greater
  • Return -1 if the value on the right is greater
The rules used by the combined comparison operator are same as the currently used comparison operators by PHP viz. <, <=, ==, >= and >. Those who are from PERL or RUBY programming background may already be familiar with this new operator proposed for PHP7.

Here's a simple example -
//Comparing Integers

echo <=> 1//ouputs 0
echo <=> 4//outputs -1
echo <=> 3//outputs 1

//String Comparison

echo "x" <=> "x"// 0
echo "x" <=> "y"//-1
echo "y" <=> "x"//1

...and so on.

Replies

You are reading an archived discussion.

Related Posts

Although, Google’s Chrome browser is the most preferred browser courtesy its great development tools but it is undeniable that there are certain palpable issues with it. One of such issues...
Attention Google Chrome browser users. You will now be able to save some of your internet data usage with the new Data Saver extension. The Data Saver extension which is...
Our digital lives dictate us to use passwords on a daily basis. Be it for checking your mail or getting access to a new application, passwords have become a norm...
Last month, Celkon had launched Millennia ME Q54 and now the Indian mobile manufacturer has expanded its Millennia series by introducing a new affordable smartphone- Celkon Millennia Q450. The company...
Bonitto.in is an extravagant online portal of exclusive Indian Ethnic Wears for Women. Bonitto was co-founded by Hardik Chaudhary and Ritesh Lahoti with an aim to become a one stop...