Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses 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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up