Introducing Null Coalescing0:00
Next up is the null coalesce operator, and you'll be thankful for this one. So it looks like this, and here's basically what it allows us to fix. Remember back in the day when you would do stuff like this, where you grab something from the $_GET super global, how about name? But now that may not exist, so you end up kind of doing something like this, where you're like, well, if that is set, then that's of course what I want. Otherwise, I either want to return an empty string, or nothing, or whatever is appropriate, right? So now if we vardump name, and we run this, we get nothing. But if we set it, or access it through a query string, equals Joe, then in that case you get it, right?
Refactoring GET Defaulting0:37
But if we set it, or access it through a query string, equals Joe, then in that case you get it, right? Pretty basic stuff, really cumbersome to write. And this has always been an annoyance with php, compared to something a little more intuitive. So instead, we could rewrite this to name equals, well, see if you have that, and if you don't, I want to use my default here. All right, so let's vardump the name, and we should get Joe, and we do. If I comment this out, we should now get nothing. It's not much more complex than that, and this is brand new to PHP 7. So in closing, in the past when you had to check to see if some kind of variable was set
Summary and Examples1:10
It's not much more complex than that, and this is brand new to PHP 7. So in closing, in the past when you had to check to see if some kind of variable was set before continuing forward, otherwise doing something else, all that tedious crap, you no longer have to do. You can just say, yeah, if I have a name variable, then just echo that out. Otherwise, I'll just use my own name. So if we run it, yeah, we see my own name up here. But of course, if we set name to John Doe, and give that another try, in that case, we'll get John Doe there. Okay, that's all there is to this one.
