About This Chapter
Conditional statements in PHP – if, else, elseif, switch, and match
Conditional statements allow you to make decisions in a program depending on whether conditions are met. Thanks to them, the application becomes dynamic and responsive to input data. In this chapter, you will learn if, else, elseif, switch, and the modern match operator.
If statement
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
}
If – else
<?php
$loggedIn = false;
if ($loggedIn) {
echo "Welcome back!";
} else {
echo "Please log in to continue.";
}
If – elseif – else
<?php
$score = 75;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 75) {
echo "Grade: B";
} elseif ($score >= 50) {
echo "Grade: C";
} else {
echo "Grade: F";
}
Switch statement
The switch
statement is useful when there are multiple possible cases to handle.
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "Almost the weekend";
break;
case "Saturday":
case "Sunday":
echo "Weekend!";
break;
default:
echo "Weekday";
}
The modern match (PHP 8+)
Since PHP 8, the match
operator has been available. It works similarly to switch
but is more concise and returns a value.
<?php
$day = "Monday";
$message = match($day) {
"Monday" => "Start of the week",
"Friday" => "Almost the weekend",
"Saturday", "Sunday" => "Weekend!",
default => "Weekday"
};
echo $message;
Best practices
- Use
===
instead of==
to avoid unexpected type conversions. - For multiple conditions, prefer
match
– it is clearer and safer. - Remember to use
break
inswitch
to avoid accidental “fall-through” between cases.
FAQ
When should I use switch and when if?
if
is used for logical conditions, while switch
is more convenient when comparing one variable to multiple values.
What’s the difference between match and switch?
match
returns a value, is more concise, and requires strict type matching.
Can conditional statements be nested?
Yes, but nesting should be used carefully – it’s often better to split logic into functions.
Chapter Lessons
Basic Conditional Statements in PHP: if, else, elseif
Learn how to use conditional statements in PHP including if, else, and elseif. Master decision-making logic with practical examples and best practices.
Switch Statement in PHP – Simple and Complete Guide
Learn how to use the switch statement in PHP. Master switch syntax, fall-through, ranges, and when to use match instead with practical examples.
The match Expression in PHP 8+: A Modern Approach to Conditionals
Learn how to use the match expression in PHP 8+. Master strict comparisons, value returns, and modern conditional logic with practical examples.
Ending Scripts in PHP: exit and die
Learn how to use exit and die in PHP to terminate scripts. Master exit codes, messages, and best practices for web and CLI applications.