Null Coalescing Basics0:00
As part of php 7, we gained access to the null coalescing operator, which is kind of a fancy term for something pretty simple to understand. It's effectively syntactic sugar over something like this, where you first want to check if a variable is set. So you might say, well, if we have a name key in the $_GET super global, then maybe we will echo that out. Otherwise, stick with a default. Yeah, so if we give that a run, there's nothing new here at all. But yeah, again, as part of php 7, we can clean this up like so. I could say, spit this out, but if it's not set, then spit out not provided.
Introducing Coalescing Assignment0:31
But yeah, again, as part of php 7, we can clean this up like so. I could say, spit this out, but if it's not set, then spit out not provided. So if we give that a run, it works. Great, really useful. So what's new in php 7.4 is the null coalescing assignment operator. Again, it sounds so fancy, and I promise you, it's not. Imagine you have a piece of data. We'll stick with User. And you know, User has a name, right? Maybe that's divided into their first and their last name.
Handling Missing Data0:58
And you know, User has a name, right? Maybe that's divided into their first and their last name. You get the idea. But as you know, whenever you're dealing with data, maybe from an API or something like that, for whatever reason, sometimes certain records just don't have pieces of data. Maybe it's null or it's not set, it hasn't been provided. So often, you're in this weird situation where you want to check the first name, and you sort of want to say, well, if there is a value for the first name, then stick with that. Otherwise use a default. So have you ever done something like this, where you say, well, set the first key or
Traditional Default Assignment1:30
Otherwise use a default. So have you ever done something like this, where you say, well, set the first key or the value for that first key equal to $value, and then we'll do a check. Is it set? Then use that. Otherwise use a default. I guarantee you've written something like this at some point in your career, right? But sure enough, if we var_dump our $user and we give this a run, it works. But if we did provide a $firstName, then we use Joe there. Okay.
Refactoring With Coalescing1:57
But if we did provide a first name, then we use Joe there. Okay. So let's upgrade this because clearly we can all agree this is horrible to write. So first up again with the null coalescing operator, we could swap all of this out, spit out this if it's set, otherwise spit out or echo out not provided. Okay. So if we give that a run, we get Joe exotic, but if first is not provided, then we reflect that with a default. Okay. Great.
Using Coalescing Assignment2:28
Okay. Great. Let's take it one more step by using the null coalescing assignment operator. We can remove this one more time and replace it with this. And now this is functionally identical to what we had originally, which is this number here. This and this are the same in php 7.4. Give it a run and it works. So cool.
