Conditional statements
Conditions
We can use so-called conditional statements to run a segment of a script only if a certain condition is met.
<?php if (true) { echo "Hi there!"; } ?>
//Output Below
Hi there!
Fantastic! The code in the braces of an if statement only runs if its condition has the boolean value of true
.
Comparison operators
That's why boolean values are so much more useful than they seem to be. For example, they're the result of comparisons.
Let's try and echo
the statement here.
<?php $hour = 9; if ($hour < 12) { echo "Hello there!"; } ?>
//Output Below
Hello there!
See that? We can use >
and <
to check if a value is greater or less than another.
Equality
We can also check whether a value or variable is equal or not equal to something.
<?php $hour = 12; if ($hour == 12) echo "Equal!"; if ($hour != 1) echo "Not equal!"; ?>
//Output Below
Equal!Not equal!
Perfect! The ==
and !=
signs are the comparison operators that check if two values are the same or not.
Else
Also, we can run code when the condition in the if statement isn't met. That's what we call an else statement.
<?php $hour = 12; if ($hour < 12) { echo "Good morning!"; } else { echo "Good afternoon!"; } ?>
//Output Below
Good afternoon!
Yes! The code in the braces of the else statement runs when the condition that comes before it returns false
.
More comparison operators
Another set of comparison operators can check for equality and inequality at the same time.
<?php $hour = 20; if ($hour >= 18) { echo "Good evening!"; } ?>
//Output Below
Good evening!
See that? The <=
and >=
signs check if a value is greater than or equal to or less than or equal to another value.
Elseif
We can also combine the else
and if
keywords to account for multiple conditions.
<?php $hour = 20; if ($hour < 12) { echo "Good morning!"; } elseif ($hour < 18) { echo "Good afternoon!"; } else { echo "Good evening!"; } ?>
//Output Below
Good evening!
Bravo! The elseif
keyword allows us to add as many conditions as we want to check for.
Logical operator &&
What if we want to test if two conditions are true at the same time, though? For that we use something called the logical and.
Which of these would satisfy what is either side of the &&
?
<?php $hour = 12; if ($hour >= 0 && $hour < 24) { echo "Triggered!"; } ?>
//Output Below
Triggered!
See that? &&
the logical and, needs both conditions met for the code inside to be executed.
Logical operator ||
When we want test if at least one condition is true, we use something called the the logical or.
Which of these would satisfy at least one condition either side of ||
?
<?php $hour = 0; if ($hour == 0 || $hour < 24) { echo "Triggered!"; } ?>
//Output Below
Triggered!
Nice. ||
the logical or operator requires only one condition to be true in the statement for the code inside to be executed.
Switching
When we want to compare a variable with many different values, switch statements might be the better option.
Let's see what happens when we give $season
a value here.
<?php $season = "Winter"; switch ($season) { case "Winter"; echo "Frozen season"; break; case "Fall"; echo "Cool season"; break; default: echo "Other season"; } ?>
//Output Below
Frozen season
See that? case
checks for a scenario, break
ends the code, and default
triggers when there are no matches.
Free-falling
What if we want multiple cases in a switch statement to trigger the same code? In that case we group them together.
Which of these might trigger "You're not a freshman"?
<?php $year = 3; switch($year) { case 1: echo "You're a freshman"; break; case 2: case 3: case 4: echo "You're not a freshman"; break; default: echo "Are you still in school?"; } ?>
//Output Below
You're not a freshman
See that? Cases without a break
keyword will fall through to the next break
keyword or to the end.
Comments