A PHP program does not always need to execute every instruction in the same way. Sometimes, it needs to make a decision based on a condition.
For example, a website may need to:
-
Allow a user to access a page only after login
-
Display a different message based on age
-
Show a discount when an order reaches a certain amount
-
Check whether a form contains valid information
-
Display different content based on a user's role
PHP uses conditional statements to handle these situations.
In the previous tutorial, you learned about comparison and logical operators. Now we will use those operators with PHP conditional statements to create real decision-making logic.
What Is a Conditional Statement?
A conditional statement checks whether a particular condition is true or false.
The basic structure is:
if (condition) {
// code to execute
}
For example:
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
}
Because $age is greater than or equal to 18, PHP executes the code inside the if statement.
Output:
You are an adult.
If the condition is false, PHP skips the code inside the if block.
The if Statement
The if statement is the simplest conditional statement in PHP.
Example:
<?php
$temperature = 30;
if ($temperature > 25) {
echo "It is a hot day.";
}
The condition is:
$temperature > 25
Since the condition is true, PHP displays the message.
You can use any valid comparison or logical expression inside an if statement.
The else Statement
Sometimes you want PHP to execute one block of code when a condition is true and another block when it is false.
This is where else is useful.
Example:
<?php
$age = 16;
if ($age >= 18) {
echo "You can access this content.";
} else {
echo "You are not old enough.";
}
Since $age is 16, the condition is false.
Output:
You are not old enough.
The else block runs whenever the if condition is false.
Using if and else Together
A common real-world example is checking whether a user is logged in.
<?php
$isLoggedIn = true;
if ($isLoggedIn) {
echo "Welcome to your dashboard.";
} else {
echo "Please log in to continue.";
}
If $isLoggedIn is true, the user sees the dashboard message.
If it is false, the login message is displayed.
This basic pattern will become very important when we build authentication systems later.
The elseif Statement
Sometimes there are more than two possible conditions.
For example, imagine a grading system:
-
80 or above → Excellent
-
60 or above → Good
-
40 or above → Pass
-
Below 40 → Fail
You can use elseif for this:
<?php
$score = 75;
if ($score >= 80) {
echo "Excellent";
} elseif ($score >= 60) {
echo "Good";
} elseif ($score >= 40) {
echo "Pass";
} else {
echo "Fail";
}
Output:
Good
PHP checks the conditions from top to bottom.
Once it finds a true condition, it executes that block and skips the remaining conditions.
Multiple elseif Conditions
You can use multiple elseif statements when necessary.
Example:
<?php
$age = 25;
if ($age < 13) {
echo "Child";
} elseif ($age < 20) {
echo "Teenager";
} elseif ($age < 60) {
echo "Adult";
} else {
echo "Senior";
}
Output:
Adult
The order of conditions is important.
PHP checks the first condition, then the second, then the third, until it finds a matching condition.
Using Logical Operators in Conditions
You can combine conditions using logical operators such as && and ||.
AND Example
<?php
$age = 25;
$hasID = true;
if ($age >= 18 && $hasID) {
echo "Access granted.";
} else {
echo "Access denied.";
}
Both conditions must be true for the first block to execute.
OR Example
<?php
$isAdmin = false;
$isModerator = true;
if ($isAdmin || $isModerator) {
echo "You can access the control panel.";
} else {
echo "Access denied.";
}
Only one of the conditions needs to be true.
The switch Statement
PHP also provides a switch statement for situations where you want to compare one value against several possible values.
Example:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week.";
break;
case "Friday":
echo "Almost the weekend.";
break;
case "Sunday":
echo "Weekend.";
break;
default:
echo "Regular day.";
}
The switch statement checks $day against each case.
Because $day is "Monday", the first case executes.
Output:
Start of the week.
Why Is break Important?
The break statement tells PHP to stop executing the switch statement after a matching case.
For example:
case "Monday":
echo "Monday";
break;
Without break, PHP may continue executing subsequent cases.
The default block is used when none of the cases match.
if/else vs switch
Both can be used for decision-making, but they are useful in different situations.
Use if, elseif, and else when working with:
-
Ranges
-
Comparisons
-
Multiple conditions
-
Logical expressions
Use switch when comparing one value against several specific possibilities.
For example:
if ($age >= 18)
is better suited to if.
While:
switch ($day)
is useful when $day can be "Monday", "Tuesday", "Wednesday", and so on.
Practical Example: Product Discount
Let's create a simple discount system.
<?php
$total = 150;
if ($total >= 200) {
$discount = 20;
} elseif ($total >= 100) {
$discount = 10;
} else {
$discount = 0;
}
$finalPrice = $total - ($total * $discount / 100);
echo "Original Price: $" . $total . "<br>";
echo "Discount: " . $discount . "%<br>";
echo "Final Price: $" . $finalPrice;
For a $150 purchase, the customer receives a 10% discount.
This is an example of how conditional statements can be used in a real web application.
Common Beginner Mistakes
1. Forgetting braces
Use:
if ($age >= 18) {
echo "Adult";
}
Keeping braces makes your code easier to read and reduces mistakes.
2. Using = instead of comparison
This:
$age = 18;
assigns a value.
For comparison, use:
$age == 18
or, preferably when appropriate:
$age === 18
3. Incorrect condition order
Consider:
if ($score >= 40) {
echo "Pass";
} elseif ($score >= 80) {
echo "Excellent";
}
The second condition will never be reached for scores of 80 or higher because they already satisfy the first condition.
More specific conditions should generally come first:
if ($score >= 80) {
echo "Excellent";
} elseif ($score >= 40) {
echo "Pass";
}
Practice Task
Create a PHP program that checks a student's score.
Use these rules:
80–100 → Excellent
60–79 → Good
40–59 → Pass
Below 40 → Fail
Create a variable:
$score = 72;
Then use if, elseif, and else to display the appropriate result.
After that, create another example using switch to display a message for different days of the week.
Conclusion
Conditional statements allow PHP applications to make decisions based on data.
In this tutorial, you learned:
-
if -
else -
elseif -
switch -
case -
default -
break -
Logical conditions
These concepts are essential for creating useful backend applications.
In the next tutorial, we will learn how to repeat code efficiently using PHP loops.
Next Tutorial: PHP Loops – for, while, do-while, and foreach