Lessons: 11Length: 46 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.4 Spaceship Operator (Combined Comparison Operator)

Cool operator; questionable name. The spaceship operator returns one of three values based upon the result of a comparison. It's not something you'll use all the time, but it's extremely useful when sorting.

2.4 Spaceship Operator (Combined Comparison Operator)

The spaceship operator is called the spaceship operator, because it kind of looks like a spaceship. It's a less than sign, followed by an equal sign, followed by a greater than sign. And many languages have a spaceship operator, and they call it the spaceship operator and that's unfortunate. In my opinion, because it doesn't tell you anything as to what that operator does. So the technical term is the combined comparison operator that means that it is going to compare the two values. And it's going to return a value based upon the result of that comparison. So it does not return a Boolean value. Instead there are three possible values and they are all integers. So the first one is if both operands are the same then that equals 0. If the left hand operator is greater than the right hand operator then that is the value of 1. If the left hand operator Is less than the right hand operator then the return value is -1. And we could use it like this. Let's say that we have a form that is used for user registration. And one of the common things to do is to have two password fields so that the user doesn't mistype their password. So let's have password1. And we'll set that just a password. Then, we will have password2. Let's also set that to password. And then inside of our code, we would want to compare these two things to make sure that they are the same. And we can do that with an if statement. So, if password one spaceship password two and if we have something other than zero, then both values are not the same. So we could say that if both of these are not equal to zero then the user typed it wrong and they need to fix it. But if the result of the combined comparison operator is 0, then of course we would want to continue on processing the form. Now admittedly this is not a very good example of using the spaceship operator. If you want to compare two values to determine if they are equal or inequal. Then of course you would want to use the equality and inequality operators. The whole idea behind the spaceship operator is so that you can perform a comparison. And you can determine if they are equal if the left side is greater than the right side or if the right side is greater than the left side. And it really shines with the you sort function. In fact that is why we have the spaceship operator to make that whole thing a lot easier to do. So let's look at that. Let's have an array of names that we want to sort. So let's have Jeremy, let's have a Steve. Then let's have a Stephen. But this is going to be a ph Stephen. Then let's have a Chris, and then a Martha and we'll just leave it with that. So we want to sort these. And if we wanted to do this correctly then we would need to do this, we'll use you sorts. Names and then our function would have a and b, and we would need to perform multiple comparisons here. We would first of all need to check to see if a is a less than b. And if it is then we will simply return negative one. But then we would also need to check to see if a is greater than b. And if it is then we would want to return positive one. Otherwise they are equal so we would return 0. And let's go ahead and var_dump names now. So whenever we run this, let's see what we gets we have our array. That is sorted Chris, Jeremy, Martha, Stephen, ph and then Steve. So this is a lot that we have to write it's also a lot that we have to remember. So wouldn't it be much easier if we could just do this return a spaceship and then b. Let's go back. Let's run the file. We get the same exact results with much simpler code. So you will want to use the spaceship operator in those cases where you need to perform a comparison of two values but you need to know whether they are equal, whether one side is less than the other. And if one side is greater than the other. If you need to perform just a simple comparison, then you don't want to use the spaceship operator.

Back to the top