- 720p
- 540p
- 360p
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
Exercise files
Download this lesson’s related exercise files.
Conditional Statements58.9 KB Conditional Statements - Solution
59.1 KB
Quick reference
Conditional Statements
Conditional statements allow us to test against conditions.
When to use
Any time you want to test against a condition and take some action based on that condition, use a conditional statement.
Instructions
The main conditional statements are IF, IF/ELSE, and IF/ELSEIF.
An If Statement takes this form:
if (condition) {
// do something
}
And if/else statement takes this form:
if (condition) {
// do something
} else {
//do something else
}
An if/elseif statement takes this form:
if (condition) {
// do something
} elseif (condition 2) {
//do something
} else {
//do something else
}
Hints & tips
- The main conditional statements are If, If/Else, and If/Elseif
- Use our comparison operators and logic operators as the conditions
- 00:04 Okay, in this video, we're gonna talk about conditional statements.
- 00:07 Conditional statements are, basically, if else statements or if else if statements.
- 00:13 And they allow us to compare two things and
- 00:15 take some sort of action based on that comparison.
- 00:18 So we've looked at comparison operators in the past.
- 00:20 We've looked at logic operators, now we finally get to actually use this stuff, so
- 00:24 let's erase all of this and let's create a couple of variables, $num1 =
- 00:29 10 and let's go, $num2 = 2.
- 00:34 This is the basic structure of an if statement,
- 00:37 it's if (condition) do something and that pretty much easy.
- 00:42 Here's where we put our conditions and
- 00:44 this is where we use our comparison operators.
- 00:46 So let's say, if ($num1 >
- 00:51 $num2) then { echo "$num1
- 00:56 is greater than $num2"; }.
- 01:01 There we go.
- 01:03 So let's save this, come over here, hit reload.
- 01:06 10 is greater than two.
- 01:07 That's correct.
- 01:08 Now, what if we change $num2 into 20?
- 01:12 We get nothing or why, all because here, we're saying if $num 1 > $num2,
- 01:17 do this, otherwise, don't do anything.
- 01:20 It tests this out, it finds this to be false and so it stops,
- 01:24 it doesn't do this line.
- 01:26 Now if we wanted to do something else, we can type in else and
- 01:31 then just like this do something else.
- 01:33 So we can, echo "$num1 is NOT greater than $num2";,
- 01:40 save this and hit reload, 10 is not greater than 20.
- 01:46 If we change this back, save it and hit reload, 10 is greater than 2.
- 01:51 This is your basic conditional statement and
- 01:54 in here we can do any sort of thing we want, greater than, equal to identical.
- 02:00 Remember that comparison operator, all our comparison operators work here and
- 02:05 also our logic operators.
- 02:06 Remember and $num1 = 10.
- 02:13 So now it we'll compare two of these things, now both of these have to be true,
- 02:17 in order for this to spit out that, otherwise, we do that.
- 02:21 You do or, we can do xor,
- 02:23 all of the things we talked about in our logic operators video.
- 02:27 That's how you do those things.
- 02:28 So that's very cool.
- 02:29 So that's if and else.
- 02:30 There's one more thing.
- 02:31 You can do an else if statement, and
- 02:34 that allows you to test more than one thing, right?
- 02:38 So if $num1 > $num2; } elseif.
- 02:45 And now we can run another conditional, $num1 == 50.
- 02:50 Then echo, let's say,
- 02:55 ($num1 is 50).
- 02:58 So if we save this, hit reload, we see 10.
- 03:01 Whoops.
- 03:04 Forgot to put, I told you we'd always forget semicolons here and
- 03:07 there, even to this day I still do.
- 03:08 Okay, so, 10 is greater than 2.
- 03:10 So let's change this around, let's change $num1 to 50.
- 03:13 If $num1 is greater than $num2, well let's just hit reload and see what happens.
- 03:19 50 is greater than 2.
- 03:21 Why didn't this get executed?
- 03:23 Well, because this was already true.
- 03:26 If this is true, this gets executed and the whole thing stops.
- 03:30 If it's not true, then it drops down and it tries this.
- 03:34 So if we change this to less than and saved.
- 03:39 50 is 50 because this is not true.
- 03:43 This condition, right here, is not true.
- 03:45 So we drop down and we try the elseif statement and
- 03:49 this is true because 50 is equal to 50.
- 03:52 So it prints this out and it stops, it doesn't do this.
- 03:55 If this isn't true, and this isn't true, then we would else that out.
- 04:00 So let's give this a try.
- 04:02 Let's go 40.
- 04:03 Save this.
- 04:04 40 is not greater than 2,
- 04:06 cuz this wasn't true, this wasn't true, so we outputted that.
- 04:11 That's a conditional statement.
- 04:12 Very, very useful.
- 04:13 You will use this a thousand times in your lifetime, more than that.
- 04:16 It's just very, very important to all programming languages.
- 04:19 And this is how you do it in PHP.
- 04:21 Conditional statement,
- 04:22 remember, you could do all your comparison operators inside these parentheses.
- 04:26 You can also do all your logic operators in there.
- 04:28 It's a great way to control the flow of your code.
- 04:31 That's all for this video and in next video,
- 04:32 we'll look at switch, which is another type of conditional statement.
Lesson notes are only available for subscribers.