‫In the last lesson, we learned about the ternary operator, and there's an even newer operator as of,
‫I believe, PHP seven that is similar to the ternary.
‫It's actually used to check if a value is null, so it's called the null coalescing operator.
‫And I'm just going to give you a quick example on how to use this.
‫So let's open some PHP tags.
‫And I'm first going to use the ternary.
‫I'm going to just have a variable called color.
‫And let's say that we want to set this to a variable called favorite color if that variable exists.
‫If it doesn't then we want to set it to blue.
‫So what we can do is say is set which we've used before just to see if a variable is set or a value
‫is set.
‫And we'll say favorite color.
‫All right.
‫And then we're going to have the ternary and say if that is set if that's true then we want color to
‫store whatever is in favorite color.
‫Else we want it to store blue.
‫And let's actually create that favorite color.
‫And we'll set that to read.
‫And now if we come down here and we echo out color.
‫And reload, then we should see red.
‫If I comment favorite color out and reload, we should see blue so we can do this exact thing using
‫the null coalescing operator.
‫So I'm going to comment that out and I'm going to set color equal to.
‫And the null coalescing operator is just two question marks.
‫So all we have to do is say favorite color to double question mark and then blue.
‫And let's go ahead and save.
‫Let's reload.
‫We get blue.
‫If I uncomment favorite color and reload, we get red.
‫So this is doing the same thing that this is doing.
‫And basically the way it works is if whatever is on the left is null, then it's going to be whatever
‫is on the right.
‫Okay.
‫So let's try it out with multiple values.
‫I'll do the the ternary first.
‫So let's say color and I want to say is set.
‫So I'm going to check for favorite color.
‫And if that's there, then we want to use favorite color.
‫Else then I want to check for another variable called second favorite color.
‫So I'm going to say is set.
‫Second.
‫Favorite color.
‫All right.
‫And then after that last that parentheses there.
‫I'm going to put another question mark and say I want to use that second favorite color.
‫Else then I want to use blue.
‫All right.
‫So let's try this out.
‫We're going to go ahead and reload.
‫And we get red.
‫If we have none none of those variables we get blue.
‫And if we have.
‫Second favorite color, which I'll set to green.
‫And reload.
‫We get we get red because it's going to look for this first.
‫But if that's not there.
‫Then we get green.
‫So we can do this same thing using the null coalescing operator by saying color, set that to favorite
‫color.
‫And then double question mark and then second favorite color and then double question mark and then
‫blue.
‫So much cleaner.
‫So let's comment this out.
‫And this should work the same way we get green.
‫If favorite color is there then we get red.
‫And if neither of them are there then we get blue.
‫All right.
‫So basically again what's happening is it's checking this.
‫If it's null then it's going to look at this.
‫If that's null then it's going to look at this.
‫And you can keep going.
‫You can have as many as many of these as you want.
‫So we'll be using this throughout the course mostly in our view files.
‫But I just wanted to kind of mention that.
‫So in the next video we're going to do another little challenge.