Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
One great power of any programming language is the ability to evaluate data and take action based off of that data using conditional statements. We'll connect the concepts of conditionals that we use everyday with the syntax used in PHP.
If Statements are one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. The expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it will ignore the statement.
Example
$a = 10;
$b = 5;
if ($a > $b) {
echo "a is bigger than b";
}
Will display "a is bigger than b" because 10 is greater than 5.
Note: when you only have a single statement, you MAY leave off the curly braces. Example:
if ($a > $b)
echo "a is bigger than b";
elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.
Example
$a = 10;
$b = 10;
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
Will display "a is equal to b" because 10 equals 10. Only a single expression block will be executed
-
0:00
One great power of any programming language
-
0:02
is the ability to evaluate data and take action based off that data.
-
0:07
For example, if you're logged in to treehouse, you can take these courses.
-
0:11
But if you're not logged in you'll be directed to sign up.
-
0:15
We evaluate the data, your logged in status, and
-
0:17
take different actions based on that data.
-
0:20
We evaluate and take actions on data by using conditional statements.
-
0:25
The most basic conditional is called an if statement.
-
0:29
We use the concept of an IF statement all the time.
-
0:32
For example, if the time is six AM, my alarm will sound to wake me up.
-
0:36
Or if it's Wednesday at three PM, I need to be in a meeting.
-
0:40
If we're hungry, we should eat.
-
0:42
If we're tired, we should sleep.
-
0:44
If it's dark, we need to turn on a light.
-
0:46
This is the same with programming.
-
0:48
If something is true, then we want to perform some action,
-
0:52
if not, then we're going to skip over that action.
-
0:55
In the last video, we learned to compare values and
-
0:59
return a true false result using the comparison operators.
-
1:03
We use those comparison results for our conditional.
-
1:06
If the comparison is true then perform some action.
-
1:10
Let's get started with our first conditional.
-
1:13
We already have the var_dump of our comparison of string one,
-
1:17
and we know that it evaluates to true.
-
1:20
So let's comment out this display of string one.
-
1:22
Then we can duplicate this line and
-
1:27
comment out the first one, and we'll change this to be an if statement.
-
1:33
If, just like with the vardump,
-
1:36
the condition that we are testing goes within parentheses.
-
1:40
But instead of finishing this statement with the semi colon,
-
1:43
we use curly braces to surround the action we're going to perform.
-
1:50
In this case, I'm going to echo >> The values match.
-
1:58
So if string one equals the string we are comparing it to,
-
2:01
which we already know that it does,
-
2:03
we're going to perform the action within the curly braces of our IF statement.
-
2:08
In this case echo the values match.
-
2:11
So let's run our script.
-
2:14
Great! We see the values match.
-
2:16
Let's change our strings slightly so that it doesn't match.
-
2:23
And now run the script again.
-
2:26
This time we don't see anything.
-
2:28
That's because our conditional now evaluates to false.
-
2:32
So it skips the action within the curly braces.
-
2:35
What if instead of just skipping over the action, we want to perform another action.
-
2:40
Like telling the user that the values are not equal.
-
2:43
We can add an l statement to this conditional.
-
2:46
After the closing curly braces, we add else.
-
2:51
And then another set of curly braces.
-
2:55
Now if the condition is true, we perform the action in the first set
-
2:58
of curly braces, and the else statement is ignored altogether.
-
3:03
If the conditional is false, we skip the first set of curly braces and
-
3:07
perform the action in the second set of curly braces.
-
3:10
So let's echo the values do not match.
-
3:16
And let's run our script again.
-
3:22
We see the values do not match, because only one set of
-
3:26
actions will be performed the statement is either true or false.
-
3:30
Either the values match or the values do not match.
-
3:34
There is one more way that we can extend this if statement.
-
3:39
We combine if and else to test for a second condition.
-
3:44
Before this else statement, we can add an else if.
-
3:53
Again, we'll want an opening and closing curly brace.
-
3:57
For this conditional, let's check if $string_one is an empty string.
-
4:02
We'll add, in parentheses, $string_one Equals an empty string.
-
4:11
If this statement is true and the string_one is an empty string,
-
4:15
let's echo '$string_one is empty';.
-
4:24
Because the variable is within single quotes,
-
4:26
we will see the variable name instead of the value.
-
4:30
So first we test if string one is equal to a specific string.
-
4:34
If that conditional is true, we display the values match, and
-
4:38
the entire IF statement is complete.
-
4:40
If that conditional is false, we continue on to our next else if condition
-
4:46
that tests if string one is an empty string.
-
4:49
If this condition is true we display, string one is empty, and
-
4:53
the if statement is now complete.
-
4:56
Finally, if both of those conditions are false,
-
4:59
we reach our else statement which displays, the values do not match.
-
5:04
Again only one action will be performed
-
5:07
even though we now have three actions available.
-
5:10
Let's test this out by running our code.
-
5:14
We see the values do not match.
-
5:16
Since the variable does not match the string in the first conditional, and
-
5:20
it also is not an empty string, we are executing the code within the else block.
-
5:26
Let's change our string back to match and run the script again.
-
5:33
This time we see the values match, because our first conditional is true, and
-
5:37
we don't even attempt to reach the other statement blocks.
-
5:41
Finally, let's reset the string one variable.
-
5:43
We'll set string one equal to an empty string.
-
5:51
Now let's run the script again, and we see string one is empty.
-
5:56
The first conditional was false, so
-
5:59
it moved on to the second conditional, which evaluated to true.
-
6:03
So the code inside the second code block was executed and
-
6:06
the if statement is complete, it does not look at the final else block.
-
6:12
Great job working through those conditionals.
-
6:14
If you'd like to review conditionals, feel free to rewatch the video.
-
6:19
Else you can continue on to the next video
-
6:22
where we'll be creating our daily exercise program.
You need to sign up for Treehouse in order to download course files.
Sign up