‫In this video we're going to look at the ternary operator, which is a shorthand for an if else.
‫And it really comes in handy, especially when you're working with HTML output and templates.
‫So in this video I'm going to show you just the basics of the syntax and how it works.
‫And we're going to take our job listings where we have the, the, the long if else for the background
‫color for the cards, for example.
‫And we're going to refactor that into a ternary operator and make it more compact.
‫So this is the ternary operator.
‫Basically we have a question mark.
‫And then we also have the the colon which is represents else.
‫So there's three parts to this.
‫There's the condition okay.
‫So the condition to to check or to evaluate.
‫And then after the question mark we have the expression or the value to return if that condition is
‫true.
‫And then after the colon we have the expression or value to return if the condition is false okay.
‫So it's if this is true then this if this is false then this.
‫So it's pretty simple to understand.
‫So let's jump into our code.
‫And like I said I have just our index.php.
‫I'll just show you the basic syntax.
‫Actually let's just get rid of that.
‫And then in the job listings where we have for instance this I want to shorten this up a bit using the
‫ternary operator.
‫So in the index let's start off by just creating a variable and I'll call it score.
‫And I'm going to set that to 50.
‫And then I'm first going to just create a regular if else.
‫So we'll say if that score is greater than 40 then let's echo out high score.
‫Okay.
‫And then in an else.
‫Let's echo out.
‫We'll just echo out low score.
‫Okay.
‫So let's let's go ahead and run that.
‫And of course we see high score because score is equal to 50.
‫And that's greater than 40.
‫So I want to rewrite this using the ternary operator.
‫So let's come down here and let's.
‫I'm trying to think if we want to, let's just echo out either high score or low score first, and then
‫I'll show you how we can store it in a variable, depending on if it's if it's true or false.
‫So let's echo and then remember the condition.
‫So we have condition expression one, expression two.
‫Or you could even say value one and value two.
‫So let's say score.
‫Is greater than 40.
‫That's the condition.
‫Then we have our question mark.
‫Also the ternary operator and what we want to do if that's true.
‫So in this case we want high score okay.
‫We already have the echo here.
‫So we're not going to put echo here.
‫It's just whatever we want to to output.
‫Else if that's false then we want low score.
‫And that's it.
‫So let's save that.
‫And if we reload, you see we get high score for this.
‫We also get high score for this.
‫So this is a short shorthand way of writing this.
‫Now, a lot of times you want to put that into a variable.
‫So instead of just doing an echo, we could say result equals and set that to that and then echo out
‫the result.
‫And we should see the same thing.
‫So now let's go to our job listings example.
‫And I want to go to where we have this right here.
‫What we're doing is checking to see actually before we do that let's load the page up.
‫So it'll be slash.
‫Job, dash listings, dot PHP and every you can see.
‫Every other one, every even card here has that light blue background because we're checking to see
‫if the index is even.
‫If it is then we have the class of BG blue 100.
‫If it's not then it's white.
‫So we can really shorten this up.
‫Let's get rid of this.
‫Make sure that you keep the the ending double quote and and the angle bracket.
‫So now if I reload they should all be white.
‫So now we want to go back into the class.
‫So right in here and let's put a space.
‫And then we'll open up our PHP.
‫And we'll close it.
‫And then in here what we're going to do is check that same condition.
‫So let's say if the index is actually let me just close that up.
‫So if the index and then modulus two equals zero then we know it's even.
‫So I'm going to use the question mark here.
‫And if it is even then I want to output the blue dash.
‫Uh, what is it?
‫BG below sorry, BG dash below 500 or 100 rather else.
‫So we use the colon for else.
‫Then I want it to be BG white and that's it.
‫So if I save that reload.
‫Doesn't do anything.
‫And the reason for that is because we need to echo this.
‫So and we don't even have to echo.
‫We could just use the shorthand like that.
‫And there we go.
‫So let's see where else we could do this.
‫We have the local badge, which is right here.
‫We have an if statement saying if it's New York, then I'll put this span and then we have our end div.
‫And if we wanted to do something else, if it wasn't local like maybe remote, then we'd have to put
‫in a PHP elseif and so on.
‫So it just it gets to be a lot of lines of code when we could shorten this up.
‫So what I'm going to do is just go right below it and rewrite this with a ternary.
‫Then I'll delete this.
‫So we'll start off by opening our PHP tag.
‫And actually we don't even need that because we're going to echo this out.
‫So we can do that.
‫Let's end it as well.
‫And then I want to check my job location or check the jobs location like that and see if it's equal
‫to.
‫New York.
‫And if it is so, I'll put my question mark here.
‫Then I want to output this span which I'm going to put in single quotes.
‫Okay.
‫And then we have our else.
‫And then whatever you want.
‫If it's not New York, you could just put an empty set of quotes.
‫So this will do the same thing as this right.
‫So we can get rid of that.
‫And save and reload.
‫And we still see our local badge.
‫Now, like I said, you might want to output remote if it's not local.
‫So what we can do is grab this span again, put it in here, and then let's change the color to green
‫and change the text to to remote.
‫Reload that.
‫And now, wherever it's not local, it's remote.
‫And we could even do this with the tags where we're checking to see if the tags are empty.
‫So I'll go underneath here and let's.
‫Do our shorthand syntax and what I'm going to check for, I'm going to put this in parentheses and say
‫if not.
‫Empty and we want to wrap.
‫Job.
‫Oops.
‫Job tags.
‫Because that's what we're checking.
‫And then outside of.
‫The parentheses here.
‫Let's put our ternary and then whatever we want.
‫If the if it's empty.
‫So sorry if it's not empty.
‫And what we want is this list item.
‫So I'm going to grab that and paste that in there.
‫Now we don't need our PHP tags here right.
‫Because we're already in we're already inside PHP tags.
‫So we can just concatenate.
‫Like that.
‫And then let's see, we want to go on the other side and get rid of that, those PHP tags and then concatenate.
‫We'll bring this up.
‫Like that.
‫And then we need our else.
‫And I'll just put just an empty, empty string.
‫All right, so if I reload this.
‫And of course, we want to get rid of the old one.
‫Reload.
‫And now we see tags wherever there are tags.
‫And if there aren't, then it doesn't show the label there.
‫It doesn't show the tags label.
‫We should probably put a space there because that's kind of.
‫Close together.
‫All right.
‫So that's a shorter way of doing it.
‫And it's up to you.
‫I mean if you if you want to use the longer form version that's absolutely fine.
‫But this is another option.
‫Now in the next video we're going to look at something that is a little newer.
‫It came out with PHP eight called the Null Coalescing operator.