This article will make you aware of a very useful operator i.e the spaceship operator PHP 7. The spaceship operator or combined comparison operator is denoted by “<=>“. This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands.
This operator has similar behavior like strcmp() or version_compare(). This operator can be used with integers, floats, strings, arrays, objects, etc.
This <=> operator offers combined comparison :
- 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
// Comparing Integers echo 1 <=> 1; // outputs 0 echo 3 <=> 4; // outputs -1 echo 4 <=> 3; // outputs 1 // String Comparison echo "a" <=> "a"; // outputs 0 echo "m" <=> "y"; // outputs -1 echo "y" <=> "c"; // outputs 1
Integers 0 1 -1 Float 1 -1 0 Strings 0 1 -1 Arrays 0 0 1 -1
- Ternary operator vs Null coalescing operator in PHP
- PHP | $ vs $$ operator
- 'AND' vs '&&' as operator in PHP
- What is the difference between the | and || or operator in php?
- Double not (!!) operator in PHP
- Nodejs | GM operator() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet