What is use of Spaceship Operator?

Technology CommunityCategory: PHPWhat is use of Spaceship Operator?
VietMX Staff asked 3 years ago

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

Consider:

//Comparing Integers
echo 1 <= > 1; //outputs 0
echo 3 <= > 4; //outputs -1
echo 4 <= > 3; //outputs 1

//String Comparison

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