0

What is Spaceship Operator (<=>) in PHP 7?

Spread the love

i don’t know you are aware of it or not but PHP 7, which will come out in November this year will introduce the Spaceship (<=>) operator. if you don’t know What is it and how does it work? then please read this post carefully.

This <=> operator will offer combined comparison in that it will :

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.

how the strings are compared?

When you compare a string this way, it will go from left to right, and compare each character in the given string to see if they are different, until it finds a difference, then it will decide which string is bigger by comparing the ASCII value of this last character. Coincidentally, since you are using only numbers, the highest numbers are also higher on the ASCII table.

This solution will work as long as you use only numbers in your comparisons, and that each string has the same number of characters

Also note that this works only since you are using the YYYY-MM-DD format, if you used another format it would not work.

Leave a Reply

Your email address will not be published. Required fields are marked *