PHP conditional statements:
if,elseif,elseswitchwithcaseandbreakmatch(PHP 8): a shorter, stricter switch that returns a value and uses===.
Conditions in PHP.
✍️ 2 exercises in this lessonPHP conditional statements:
if, elseif, elseswitch with case and breakmatch (PHP 8): a shorter, stricter switch that returns a value and uses ===.Mastering PHP If...Else & Match helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
<?php
$hour = (int) date("H");
if ($hour < 12) {
echo "Good morning";
} elseif ($hour < 18) {
echo "Good afternoon";
} else {
echo "Good evening";
}
$status = 404;
echo "<br>" . match ($status) {
200 => "OK",
301, 302 => "Redirect",
404 => "Not Found",
default => "Unknown",
};
Good afternoon Not Found
Check what you learned in “PHP If...Else & Match”.
PHP's "else if" keyword.
if ($a > 10) {
} ($a > 5) {
}
What does match (PHP 8) use to compare values?
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services