Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Null-Coalescing Operator

Many object-oriented languages have null-coalescing operators, and PHP can finally count itself as one of them. It's a very helpful operator, and you'll learn how to use it in this lesson.

2.3 Null-Coalescing Operator

Many languages have a null coalescing operator. And as of version 7, PHP can finally boast that it is one of those languages. Now the idea behind the null coalescing operator is very simple. It is a pair of question marks and it works with two operands. So let's look at this code. Let's say that we have a URL parameter called ID, and it's optional, so we don't know if it's there or not. So usually we would use the isset function. We would pass in the GET collection, retrieving the ID value, and if that existed then we could use a ternary operator in order to retrieve that value, otherwise we would have a default value. Well, the null coalescing operator makes it so that we don't have to type all of that and instead we simply just do this. We'll use the GET collection. We will want the ID value. If this is set or if it has a value, then that value is returned to our id variable, but if it is not set or if it's null, then the second operand is returned. So in this case, if we have an id URL parameter, then that value is retrieved and assigned to our variable, otherwise the value of 1,000. So we could write this in a variety of different ways. We could use the ternary operator, or we can have an if statement, but the null coalescing operator makes it a lot cleaner. So let's look at this in the console and we're just going to dump the id variable. And we will see that it is an int with a value of 1000, because at the time that this ran we didn't have a URL parameter called id. Now this isn't all. I mean, yes, we are working with two operands here, but we can chain these together. So that the first thing we could look at is the URL itself. And if the id parameter was in the URL, then great, we have that value. And we just go on about our business. But if not, then we can say that, okay, check the post data for our id. And if that value was there, then it would be returned. Otherwise, we would go on to the next operand in our statement which would be our default value. So in this case of course once again we are going to see an int of a value 1000. Now, of course I'm using GET and POST just for the sake of an example here. You can use whatever value or object that you want for your operands. So if you have a variable called foo then you could do that. And in fact let's go ahead and let's set a value for foo. So of course in this case it's kind of pointless to do this but for the sake of demonstration, let's go and look at what happens whenever we execute this. We have an integer value with a value of 10. But if we set this as null and we go back, we run, then we have a value of 1000. So we can use whatever value that we want. It doesn't have to be just from the GET or POST collections but that is probably going to be the most common usage. And really that's it as far as the null coalescing operator is concerned. It's just syntactic sugar for performing the same operations that we have been.

Back to the top