PHP If...Else & Match

Conditions in PHP.

✍️ 2 exercises in this lesson

PHP conditional statements:

  • if, elseif, else
  • switch with case and break
  • match (PHP 8): a shorter, stricter switch that returns a value and uses ===.

Key Takeaways: PHP If...Else & Match

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 If...Else & Match Code Example

PHP
<?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",
};
Output
Good afternoon
Not Found
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

PHP If...Else & Match Practice Exercises

Check what you learned in “PHP If...Else & Match”.

/ 2 solved
Fill in the blank

PHP's "else if" keyword.

if ($a > 10) {
}  ($a > 5) {
}
✓ Correct! Not quite. Try again.
Multiple choice

What does match (PHP 8) use to compare values?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

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