Conditional Statements in PHP
What Are Conditional Statements?
Conditional statements allow your PHP script to make decisions. Based on whether a condition is true or false, the program executes a block of code.
This enables dynamic behavior — for example, showing a welcome message to logged-in users, or redirecting users who haven’t completed a form.
Using if
, else
, and elseif
Basic if
Statement:
<?php
$age = 18;
if ($age >= 18) {
echo "You are eligible to vote.";
}
?>
if-else
Statement:
<?php
$isMember = false;
if ($isMember) {
echo "Welcome back!";
} else {
echo "Please sign up.";
}
?>
if-elseif-else
Statement:
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade: A";
} elseif ($marks >= 75) {
echo "Grade: B";
} elseif ($marks >= 50) {
echo "Grade: C";
} else {
echo "Grade: F";
}
?>
This structure is useful when you have multiple possible outcomes.
Real-World Example: Login Status
<?php
$isLoggedIn = true;
$username = "Emma";
if ($isLoggedIn) {
echo "Welcome, $username!";
} else {
echo "Please login to continue.";
}
?>
This is a common pattern used on websites that manage user accounts.
Nested Conditional Statements
You can also place one condition inside another — this is called nesting.
Example:
<?php
$role = "admin";
$status = "active";
if ($role == "admin") {
if ($status == "active") {
echo "Access granted to admin dashboard.";
} else {
echo "Your account is not active.";
}
} else {
echo "You are not an admin.";
}
?>
Although nesting is powerful, it’s best used sparingly to keep code readable.
Using switch
Statement in PHP
The switch
statement is a cleaner way to compare a single variable against multiple values.
Syntax:
switch (variable) {
case value1:
// code to execute
break;
case value2:
// code to execute
break;
default:
// default code
}
Example:
<?php
$day = "Saturday";
switch ($day) {
case "Monday":
echo "Start of work week";
break;
case "Saturday":
case "Sunday":
echo "It's weekend!";
break;
default:
echo "Midweek day";
}
?>
The switch
block is especially useful when checking the value of enums, request types, or specific user actions.
Common Mistakes to Avoid
- Forgetting to use
==
or===
in comparisons. - Missing
break
statements inswitch
, which causes fall-through. - Using assignment
=
instead of comparison==
inif
conditions. - Writing overly nested conditions that are hard to maintain.
Best Practices
- Use strict comparison (
===
) where type safety matters. - Avoid deep nesting by combining conditions logically.
- Use
switch
when checking against multiple fixed values. - Keep conditionals readable with proper indentation and spacing.
- Use parentheses to clarify complex logical expressions.
Notes:
- Conditional statements let your code make decisions.
- Use
if
,else
, andelseif
for simple branching. - Use
switch
when evaluating a single variable against many values. - Avoid unnecessary nesting and write clear, readable conditions.
Practice Tasks
Task 1: Age Verifier
Create a PHP script that checks a person’s age and prints:
- Under 13: “You are a child.”
- 13–17: “You are a teenager.”
- 18+: “You are an adult.”
Task 2: Payment Status
Set a $status = "paid"
variable and print:
- If “paid” → “Payment successful”
- If “pending” → “Awaiting confirmation”
- If “failed” → “Payment failed”
- Else → “Invalid status”
Task 3: Day Checker
Use switch
to print messages for each day of the week.
Example: “Today is Monday. Let’s be productive!”
💡 Explore More PHP Learning Tools & Resources
PHP Practice Quiz
Test your PHP skills with real coding questions and scoring.
PHP Interview Questions
Prepare for interviews with common PHP questions and answers.
Educational AI Tutor
Ask PHP questions and get instant AI-powered explanations.
Question Generator
Auto-generate PHP interview questions for quick practice.