Introducing Conditionals 0:00Welcome back. So now, rather than talking about functions, I know I said we would cover that, I'm going to put that off one more video, and we'll instead tackle conditionals. So here's the code we wrote in the last lesson. We created a task and made it equal to an associative array, and then we built up a set of HTML based upon the values in that array. And, real quick, person responsible, not personal. Anyways, we now want to add a conditional here that says, if the task is completed, I want some kind of check mark, and if it's not completed, I don't know, maybe something else, maybe nothing at all. Okay, well, once again, we use a conditional for that. Now, in fact, you had your first taste of the conditional in the last lesson. This is what we call the ternary operator, and that, in effect, is a condition. Check the value of this, and if it's true, do this, and if it's false, do that. Ternary vs If/Else 0:42the conditional in the last lesson. This is what we call the ternary operator, and that, in effect, is a condition. Check the value of this, and if it's true, do this, and if it's false, do that. But now let's review the long form of that. We can say if. So if some kind of condition, if that turns out to be truthy, well, proceed in some form, otherwise, else, do something else. So why don't we try this? Why don't we take our ternary? This is kind of a nice and short way to do it, but we're going to change it up. We're going to do it in the long form. So we'll say php if task completed. Well, we can use the braces. So for example, we can format it like this, and you can even use echo here if you want. So you don't always have to close out php if you don't want. If you can get away with an echo, that's fine. So you could say echo complete, else echo incomplete. This is sort of the long way to write it, but it's valid, and then closedon't want. If you can get away with an echo, that's fine. So you could say echo complete, else echo incomplete. This is sort of the long way to write it, but it's valid, and then close it out. So this would be an option. If I come back and refresh, we still get the exact same thing, or we could change this to finished. We still get incomplete, but if we change the status to true, now it should say finished, and it does. Okay, so that, yeah, that's a condition. It's not any more complicated than that. If some condition turns out to be true, then proceed in one way. And by the way, if that's all you ever need to do, you don't need the else section. So this is perfectly valid, but sometimes you need to say, yeah, if it's true, do this. If it's false, then do that, and this would be one way to do it. But now notice that, yeah, this is very simple. Each operation is one line, and that's specifically why we use the ternary operator.then do that, and this would be one way to do it. But now notice that, yeah, this is very simple. Each operation is one line, and that's specifically why we use the ternary operator. It's really great when you just want to echo out a string if it's true or another string if it's false. So that's why we did something like this. Echo out and check the value of the completed item, and if it's true, echo out finished, and if it's false, echo out incomplete. Notice how much simpler that is rather than doing what we had here. So that's why we did it in this way, but they are both functionally equivalent. Now what you'll find, though, like I said, is if you do want to add an extra bit of HTML to add some kind of icon, well, yeah, this is where it gets kind of gross, and you probably don't want the ternary. So I'm going to hit Command-Z a few times to undo, and then I will delete this. Okay, so if it's completed, why don't we echo out some kind of Displaying Icons Conditionally 3:11kind of gross, and you probably don't want the ternary. So I'm going to hit Command-Z a few times to undo, and then I will delete this. Okay, so if it's completed, why don't we echo out some kind of completed check mark. Now why don't we do this? We could grab an icon set, but there are HTML unicodes we could reference, so those are like little built-in icons. So why don't we do that? How about this little guy right here? Now you'll notice if we try it out, here is how we would represent that. So why don't we copy that, close this out, and if it's completed, we're going to echo that. Okay, give it a refresh, and there you go. It's done. But if it's not done, then what are we going to do? Yeah, we could do it as incomplete. We could do another icon. We could do anything you want. So notice that in this case, we could still get away with the ternary operator, but yeah, in situations where maybe you have to wrap it within a span, maybe you have to give it a class Shorthand If in HTML 4:02you want. So notice that in this case, we could still get away with the ternary operator, but yeah, in situations where maybe you have to wrap it within a span, maybe you have to give it a class in order to style it, yeah, in those situations, you don't want a ternary. You want a more traditional condition. And in this case, we're still going to get the same thing. Let's bring it back to true. Yeah. What about where you don't want to echo it? Remember when we talked about earlier how if you have a lot of markup you want to echo out, it gets kind of gross. I'd like to switch back to regular HTML. So just like we can do the shorthand for a for each statement, you can do the same thing for an if. We could do this. Let's bring this all back onto the same line, and then use that colon trick that you learned about earlier. And then at the bottom, end, not for each, but we are ending an if statement. Okay. So now I can grab all of that. We don't need a span in thisthat colon trick that you learned about earlier. And then at the bottom, end, not for each, but we are ending an if statement. Okay. So now I can grab all of that. We don't need a span in this case, so I can just leave it like that. But notice we're using regular HTML here. Okay. Let's come back, refresh. We still get the exact same thing. But yeah, now you can control your HTML a bit better. You can format it better. That's a good way to go. Now you might be thinking, well, we lost our else statement. Okay. Fair enough. Else, maybe you have an incomplete icon or just the text here. And then finally, we'll say end if. Okay. So refresh. We get our check mark. But if this completed value or Boolean is set to false, then we'll get incomplete. And that's great. So good job. You're making a lot of progress here. Now, before we finish up, there is one thing. What about if you want to check if something is not true? Well, you use a bang for that. So something like this. Using Not Operator 5:45making a lot of progress here. Now, before we finish up, there is one thing. What about if you want to check if something is not true? Well, you use a bang for that. So something like this. You could say, if true, then proceed. Or if it's not true, represented by the bang or the exclamation point, then we would say here echo incomplete. And you'll do this quite a bit. So you have a value and you say, well, if this value is false, well, if that's the case, then I want to proceed in this way. So just remember, if you ever need to do that, this is the syntax you'll use. When you see exclamation point, think not. If it's not true, or we could substitute the completed value, if it's not completed, then echo complete. And that's all you need to know. All right. So that is Conditionals 101. For your homework, I want you to keep playing around with this. So maybe go back to your task and add another Boolean item, and then check the value of that Boolean Homework Assignment 6:40that is Conditionals 101. For your homework, I want you to keep playing around with this. So maybe go back to your task and add another Boolean item, and then check the value of that Boolean and echo it onto the page in some way. And with that, I will see you in the next lesson.