در حال بارگذاری ...

Introducing Conditionals0:00

Okay, welcome back. So now that you're a bit more comfortable with variables, I think the next topic we should tackle is conditionals. Let's get going, and I have an example for you. And what I'd like to do here is dynamically render a message that indicates whether or not you've read a particular book. So let me grab one real quick. All right, here's the closest one. What do we have here? We have Dark Matter.

Making Book Name Dynamic1:42

All right, that's all the CSS we're going to write here. So I will select that, switch back to PhpStorm, and then in my head tag, I'll do it inline there. And we'll say on the body tag, apply these styles. And now if we switch back and give it a refresh, yeah, it just looks a little cleaner to my eyes. Okay, so now we want to make this dynamic. So how would we do that? Well, let's begin by opening PhpText. So hmm, I think the first thing we should do is turn the name of the book into a variable.

Well, let's begin by opening PhpText. So hmm, I think the first thing we should do is turn the name of the book into a variable. So let's do that now. We'll call it $name equals a string, and then again, we conclude it with a semicolon. Now let's reference that variable. Clear this out, open php, echo $name, and then close php. Okay, come back, give it a refresh, and yeah, we get the exact same thing. Very cool. Okay, so now the next thing we need to do is indicate in some way whether or not the user has read Darkmatter.

Adding Read Status Flag2:37

Okay, so now the next thing we need to do is indicate in some way whether or not the user has read Darkmatter. So again, in many cases, this will come from a database. You'll have a database of the user and the books they've read. Have they read it? Have they not? Or sometimes you'll make an API call. Think of that as sort of like an external service that has all of this data. But yeah, right now in our learning, why don't we just hard code this? So has Darkmatter been read?

But yeah, right now in our learning, why don't we just hard code this? So has Darkmatter been read? I'll call it read. And what would I say here? Yes or no? We could do something like this, but generally, we use what's known as a Boolean. Think of a Boolean as simply true or false. Has it been read? True or false? In our case, why don't we say true?

Explaining If Statements3:48

All right, great job if you figure that out. But if not, no worries, I wouldn't blame you because we haven't yet reviewed what's known as a conditional. So think of a conditional as a way to create a branch in your logic. Or put more simply, think of it as a way to ask a question. And usually it starts with the keyword if. If such and such turns out to be the case, then I want to do this. But if it's not the case, then I want to do something else. So maybe you're going to lunch, maybe you're going to Subway. If Subway is open, then let's go in and eat.

So maybe you're going to lunch, maybe you're going to Subway. If Subway is open, then let's go in and eat. But if Subway is not open, let's go somewhere else, right? It's a simple question. And we can write that with php by saying if, and then open (. And yeah, real quick, notice how when I did that, my editor automatically added a closing ). Most editors will do that these days. All right, next, ask your question here, and then open a curly brace. And once again, my editor adds the closing curly brace.

All right, next, ask your question here, and then open a curly brace. And once again, my editor adds the closing curly brace. So think of everything that occurs between these curly braces as the logic for what should happen if this question returns true. If this question has a positive response. But in our case, yeah, we can't use basic English here. We have to speak in terms of the language. So in our case, I really just want to say, well, if the book has been read, then prepare a message. So let's clear this out.

a message. So let's clear this out. And I'm just going to say, if $read, or in other words, if the value for this variable is true, then create a message. And maybe this message will say, well, exactly what we have here. You have $name. Just like that. Okay, so now, if I come down here, I'm going to replace all of this with an opening php tag that echoes the message itself. Okay, but now, before we review this in the browser, it's going to work.

Well think about it. What do you think would happen? And if you're working along, pause the video and ask yourself that. What would happen here? Hmm. All right, let's come back. Give it a refresh. Aha! We get a warning. Undefined variable message.

We get a warning. Undefined variable message. But now, you might be thinking, wait, wait, wait a minute, wait, wait, wait, wait, wait. We created our message variable right here. So why on earth is it saying undefined when I have proof that we defined it right here? Okay, and this is what being a programmer is. You need to go through it line by line to make sure you understand what's happening. So let's do it together. We start by creating a name that evaluates to dark matter. Fine.

We start by creating a name that evaluates to dark matter. Fine. Then we have another variable called read that is set to false. And then we say, well, if read is true or positive, then, keyword there, then create a variable called message. Okay, so let's say that again. If read is true, only on that condition do we create a variable called message. But now, read is set to false, which means this condition, this question, fails. If read, no, it hasn't been read, so don't do anything here. That's what's happening.

Handling Else Case7:26

If read, no, it hasn't been read, so don't do anything here. That's what's happening. At no point in this logic do we create the $message variable, which is why php is squawking at you. Okay, so why don't we do this instead? I'm going to say else, and then once again, between these braces is the logic that should happen if the book has not been read. And in that case, why don't we duplicate this $message variable and say, you have not read the name of the book. Okay, so now the $message that we display to the user is dynamic.

Shorthand Echo and Warnings8:00

the name of the book. Okay, so now the message that we display to the user is dynamic. So if I come back and give it a refresh, you have not read dark matter. But if we toggle this to true, come back, refresh, you have read dark matter. Pretty cool. All right, so let's finish up by making just a couple small tweaks or a couple small refactors. First up, right down here where we echo out the message. No problem whatsoever. But as you can imagine, in php, echoing the value of a variable is really, really common. So with that in mind, there is a shorthand version, and it looks like this.

But as you can imagine, in PHP, echoing the value of a variable is really, really common. So with that in mind, there is a shorthand version, and it looks like this. Okay, so let's talk about it. Actually, real quick, let me prove it to you. So let's refresh, and now we have, you have read darkMatter two times. So notice, less than question equals. That is identical to opening PHP and calling echo. It's the exact same thing. So if it feels weird to you and you don't want to use it, all right, fine, no problem. But I will tell you, most people will reach for this.

So if it feels weird to you and you don't want to use it, all right, fine, no problem. But I will tell you, most people will reach for this. And then also notice how I was able to omit the semicolon. And that's because immediately after we echo the variable, we instantly close PHP. So in those situations, the semicolon is optional. Keep it if you want. It might be a good idea right now just to get you in the habit of always concluding a statement, so to speak, with a semicolon. But do know, if you ever see it being omitted, that's why. Okay, so from now on, whenever I need to echo out a variable, I will almost always reach

But do know, if you ever see it being omitted, that's why. Okay, so from now on, whenever I need to echo out a variable, I will almost always reach for this particular syntax. And in fact, I'll remove the semicolon. All right, last little thing before I let you go. Notice we have one more squiggly line here. Condition is always true because read is evaluated at this point. All right, so think about what's happening here. It's helping you out. It's saying, hey, you're asking this question if the user has read the book.

It's helping you out. It's saying, hey, you're asking this question if the $user has read the book. But because you set this $variable to true, that means 100% of the time, the answer to that question is going to be yes or true. Or in other words, if the answer is always yes, then this logic here where we proceed if the answer is not yes, we'll never fire. It's superfluous. Why does it exist? Delete it. And then once again, if the answer is always yes, then why are you asking the question?

ConditionalsBooleansShort Echo Tags

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟