‫Okay.
‫So we're going to start to get into control structures and just control flow.
‫And we're starting off with if statements and different variations.
‫We have the if statement the if else nested if and if else if.
‫Okay.
‫Now I realize that a lot of you probably already know this stuff.
‫So if you want to skip skip around these videos, that's fine.
‫If you're new to PHP or are brand new to programming, these concepts are very, very important to understand.
‫And what we're doing in this video is pretty simple.
‫So we're going to just do some basic if statements here.
‫And the next video I'm going to show you how to how to use control structures within an HTML template
‫because there are shorthands that we can use.
‫But let's go ahead and just start off with a basic if statement.
‫So the way they're formatted is we say if with some parentheses we open some curly braces and then some
‫kind of condition goes in here, some kind of condition that we can evaluate and then whatever is in
‫here, this is the code to be executed.
‫If the condition is true.
‫So that's the basic format or the basic signature of a of an if statement.
‫So let's do this.
‫Let's set a variable up here called age and we'll set it to 30.
‫And then for the condition let's say if the age is greater than or equal to 21, then we're going to
‫echo oops, we're going to echo out a string and say you are.
‫Old enough to drink in the US.
‫So if I go ahead and save that, and if I load that in the browser, we're going to see the string.
‫You are old enough to drink in the US.
‫If I change this to something that is less than 21, if I change it to 20, then we're not going to
‫see anything because this code only executes if what is in here is true.
‫Now we can also have an if else.
‫So what I'll do is grab this.
‫Paste that there, and then we'll add on to this and else, and then we'll just copy that.
‫And we're going to say else you are not.
‫Not old enough to drink in the US.
‫And then I'm just going to comment this out.
‫All right.
‫So let's run that.
‫And we see you are not old enough to drink in the US.
‫If I change this to 25 reload we get.
‫You are old enough to drink.
‫Okay.
‫So whatever is in here, we'll execute.
‫If this is not true.
‫Now we can also nest if statements.
‫We might want to check for multiple conditions.
‫So let's say not only do we want to check to see if they're old enough to drink, but we want to see
‫if they're old enough to vote, which is 18.
‫So I'll just grab that.
‫And then let's go into this into the else.
‫And we're going to add another if statement in here.
‫Another if else.
‫So we'll say if the age and the reason I'm putting it in here is because we know that this only gets
‫executed if they're older than 21.
‫So we want to check if they're if they're 18 within the else not up here.
‫So let's say if the age is greater than or equal to 18 then let's copy that, paste that in and just
‫change this to vote.
‫Vote.
‫And then we'll say else.
‫And then what I want to echo here is you are.
‫You are not old enough to drink or vote in the US because they're under 18.
‫So let's comment that out.
‫Let's reload and we see you are old enough to drink.
‫You could you could also put, you know, you're old enough to drink and vote.
‫Like that.
‫All right.
‫Now, if I change this to, let's say, 19, reload, we get.
‫You are old enough to vote in the US.
‫If I change this to 16 and reload, we get.
‫You are not old enough to drink or vote in the US.
‫Now nesting if statements can get kind of messy, especially if you have multiple nests multiple levels.
‫So we do also have an else if.
‫So what I'm going to do is grab.
‫We'll just take this.
‫And instead of.
‫Doing this.
‫Just get rid of that.
‫Instead of doing that, I'm going to have an else if here, which will look for another condition.
‫So we'll say if age is greater than or equal to 18, then whatever I put in here will be executed.
‫So let's grab.
‫This string.
‫So you're old enough to vote in the US, and then when we have an else that's going to be if this and
‫this are not matched.
‫So that would, that would be this echo here.
‫So now I'm just going to echo out a line break in between these two so we can separate them.
‫Oops.
‫And reload and we should get the same exact thing for both.
‫So we have age set to 16.
‫So you are not old enough to drink a vote.
‫Set it to 19.
‫You're old enough to vote.
‫Set it to 30.
‫You're old enough to drink and vote.
‫So these two blocks, this and this are the same thing they do.
‫They have the same logic.
‫However, this, if you ask me, looks cleaner because we're not we're not nesting.
‫We're just using else ifs to look for separate conditions.
‫Now, if you have a ton of different conditions, there is something called a switch statement, which
‫we're going to get into a few videos from now.
‫But in the next video, I want to show you how to use if statements within HTML, because there are
‫some shorthands that we can use.