Next up, let's talk about the control structures, starting with the if statement.
So as the name suggests, the control structures are used to control the flow of the program.
And in the case of the if statement, you just conditionally run some statements if the expression that
is being evaluated is true.
Now you can also provide alternative cases using else if you can check for alternative conditions.
If this first condition was false, and you can add as many as those alternative paths as you need,
and only one of those will run and they will be checked until there is no other alternative condition.
And optionally you can also add an else statement which will be run if none of those above conditions
evaluated to true.
As a quick reminder, the expression is something that always returns a value.
That's what differentiates the expression from statement.
Statement can be an expression, but it can also be a control structure like the if statement, which
in this case means that if statements can be nested.
Okay, let's do some practical exercises now.
Okay.
So let's jump into the code editor and create a new file.
I'm going to call it FP as that's the topic of this video.
Let me add a PHP tag.
And immediately let's start with the first example.
So for that I'm going to just declare a variable.
Let's assign the value of ten to it.
And let's write a simple if statement.
So it starts with the if keyword, then we've got parentheses that's mandatory.
And inside it we need to add an expression that will evaluate to boolean value.
So the condition inside the parentheses must evaluate to a Boolean value because we essentially have
two choices.
So either this conditional statement that depends on this condition inside parentheses will run or it
won't.
And that's why it needs to be a boolean.
Now I'm adding a curly braces.
So first let me add a condition here.
I'm gonna just check if x is bigger than five.
So currently I think we can be sure that that's true.
But that's the thing with variables.
Variables can change.
And there might be a different value here that needs to be verified.
Now I'm just going to say x is greater than five.
And let me add a new line here and let's run it.
That's BHP.
If BHP.
Now obviously in this case ten is greater than five.
Most people prefer this convention of always using curly braces, but they are not always mandatory.
So if you would only have one statement inside your if control structure, then you can skip the curly
braces.
So this is also fine.
And I can confirm that by running this code.
So I'm gonna use the curly braces everywhere.
I think it is a good convention.
It makes the code much more clear, especially if you have nested if statements.
So this statement only runs if this condition is true.
But if this condition is false, then nothing happens.
So let's see another example where we're going to have some alternatives.
And this is calculation of the grade for a student based on his score in points.
So let's say the score is 85 out of 100 points possible.
Now we need to give students the grade.
So we check for the score.
If it's more than 90 we're going to just echo a.
Keep in mind this here is not one character.
Those are actually two characters more than followed immediately by equality sign.
It's just called a ligature.
And this is how the text editor just renders that those are two characters.
Okay.
So now I want to provide an alternative, because we can expect that it's quite possible that a lot
of people will have a score lower than 90.
So in this case, I'm gonna check if maybe the score is more than or equal 80.
And then I'm going to give it a B.
So let me copy paste that two more times.
So we've got different thresholds in here 7060.
This results in C and this results in D.
Now for all other cases when someone is below 60 there is no point in actually adding a specific condition
for this.
And in that cases we can just go with else.
So for all the other cases then you're getting an F.
So this might also cover a case when someone doesn't have a score, because maybe he just didn't show
up for the exam.
And then he gets F Okay, so now let's run it.
We do PHP if PHP and in this case the score is B, if I change the score to, let's say 60 to be on
the edge, this is D.
Okay.
So that was another example.
Now let's see how can we use nested if statements.
So next up let's define a variable that will be just called num as a number.
And we'll try to say something about the number that is currently inside the variable.
So the first condition here is checking if the number is something positive.
So something more than zero.
And if that's not the case we just echo non A positive number.
But if it is positive, we're going to check if this is an even or an odd number.
Now, the easy way to check if the number is even or odd is to use the modulo operator, which just
gives you a remainder of division by two.
So if in this case that is zero, then we know we have an even number.
So we can say a positive even number.
And in the other case we just used else and we say echo positive odd number.
Now let me add a new line character before every single one of those strings and after.
So that we just have a clear output.
And now let's run this if script.
So currently we've got a positive odd number.
That's the case.
Now let's rerun it again by changing the number to 14.
That's a positive even number.
Yes that's correct.
And minus three this is a non positive number.
You should know that in control structures which just have one statement inside their body.
And the body is basically everything between the curly braces.
So that's a body.
For example if there is only one statement inside you don't need to use curly braces.
So now I can remove all of those curly braces here and everything would still work fine.
Now most people would prefer the convention to always add curly braces, But just so you know, it is
just a convention.
And this code here is totally fine.
And it would work and I can run it and everything is fine.
So just so you know, this nested if statement is basically considered bank one statement.
So I'm going to bring it back to using curly braces.
But well you should know about all the possibilities.
And another benefit of using the curly braces is that I get this highlighting in the code editor of
the matching curly braces.
This really helps if you have bigger blocks of code that might be longer, and might really be longer
than what you currently see on the screen.
Okay, so let's have a final example with the username and password.
So let's define two variables.
The username, which would be admin, and the password which can be just password one two, three.
Now we're gonna add an if statement and we'll check if the username is specifically admin.
And we're going to add a logical operator because we also need to make sure that both are true.
So username is exactly admin and that also password.
That's why we use this.
And operator that password is exactly password 123.
So if that's fine we just say success.
And otherwise you can say failure.
I'm missing the echo statement here.
Now just keep in mind that in real applications you will have the users and the passwords stored inside
the database, and they would not be stored as plain text.
They will be encoded.
So this here is just a simple example.
So we can showcase more examples with the if statements.
Now let's run that in the terminal.
And we have a success.
And if I would change this password even slightly we should have a failure.
Okay.
So let's summarize.
So we've seen some new operators like this logical operator.
We've had examples of the if statements by having the else alternatives and also else if alternatives.
We've used the nested if statements.
And also we learned that the control structures, if there is just one statement inside their body,
you can skip the curly braces.