Okay, in this video, we're gonna talk about conditional statements.
Conditional statements are, basically, if else statements or if else if statements.
And they allow us to compare two things and
take some sort of action based on that comparison.
So we've looked at comparison operators in the past.
We've looked at logic operators, now we finally get to actually use this stuff, so
let's erase all of this and let's create a couple of variables, $num1 =
10 and let's go, $num2 = 2.
This is the basic structure of an if statement,
it's if (condition) do something and that pretty much easy.
Here's where we put our conditions and
this is where we use our comparison operators.
So let's say, if ($num1 >
$num2) then { echo "$num1
is greater than $num2"; }.
There we go.
So let's save this, come over here, hit reload.
10 is greater than two.
That's correct.
Now, what if we change $num2 into 20?
We get nothing or why, all because here, we're saying if $num 1 > $num2,
do this, otherwise, don't do anything.
It tests this out, it finds this to be false and so it stops,
it doesn't do this line.
Now if we wanted to do something else, we can type in else and
then just like this do something else.
So we can, echo "$num1 is NOT greater than $num2";,
save this and hit reload, 10 is not greater than 20.
If we change this back, save it and hit reload, 10 is greater than 2.
This is your basic conditional statement and
in here we can do any sort of thing we want, greater than, equal to identical.
Remember that comparison operator, all our comparison operators work here and
also our logic operators.
Remember and $num1 = 10.
So now it we'll compare two of these things, now both of these have to be true,
in order for this to spit out that, otherwise, we do that.
You do or, we can do xor,
all of the things we talked about in our logic operators video.
That's how you do those things.
So that's very cool.
So that's if and else.
There's one more thing.
You can do an else if statement, and
that allows you to test more than one thing, right?
So if $num1 > $num2; } elseif.
And now we can run another conditional, $num1 == 50.
Then echo, let's say,
($num1 is 50).
So if we save this, hit reload, we see 10.
Whoops.
Forgot to put, I told you we'd always forget semicolons here and
there, even to this day I still do.
Okay, so, 10 is greater than 2.
So let's change this around, let's change $num1 to 50.
If $num1 is greater than $num2, well let's just hit reload and see what happens.
50 is greater than 2.
Why didn't this get executed?
Well, because this was already true.
If this is true, this gets executed and the whole thing stops.
If it's not true, then it drops down and it tries this.
So if we change this to less than and saved.
50 is 50 because this is not true.
This condition, right here, is not true.
So we drop down and we try the elseif statement and
this is true because 50 is equal to 50.
So it prints this out and it stops, it doesn't do this.
If this isn't true, and this isn't true, then we would else that out.
So let's give this a try.
Let's go 40.
Save this.
40 is not greater than 2,
cuz this wasn't true, this wasn't true, so we outputted that.
That's a conditional statement.
Very, very useful.
You will use this a thousand times in your lifetime, more than that.
It's just very, very important to all programming languages.
And this is how you do it in PHP.
Conditional statement,
remember, you could do all your comparison operators inside these parentheses.
You can also do all your logic operators in there.
It's a great way to control the flow of your code.
That's all for this video and in next video,
we'll look at switch, which is another type of conditional statement.