PHP — If Else
Basic if statement
Execute code only when a condition is true:
<?php
$temperature = 28;
if ($temperature > 25) {
echo "It's warm outside!";
}
?>
The condition is evaluated as a boolean. PHP treats these values as falsy:
<?php
// All falsy values:
false
0
0.0
""
"0"
null
array()
// empty object (PHP 8+)
?>
Everything else is truthy, including non-empty strings, non-zero numbers, and arrays with elements.
if-else
Provide an alternative path:
<?php
$hour = 14;
if ($hour < 12) {
echo "Good morning!";
} else {
echo "Good afternoon!";
}
// Output: Good afternoon!
?>
if-elseif-else
Check multiple conditions in sequence:
<?php
$score = 85;
if ($score >= 90) {
$grade = "A";
} elseif ($score >= 80) {
$grade = "B";
} elseif ($score >= 70) {
$grade = "C";
} elseif ($score >= 60) {
$grade = "D";
} else {
$grade = "F";
}
echo "Your grade is: $grade"; // B
?>
Important: PHP evaluates conditions top-to-bottom and enters the first matching block. Conditions are mutually exclusive — only one block executes.
Ternary operator
Compact if-else for simple assignments:
<?php
$age = 20;
$label = ($age >= 18) ? "Adult" : "Minor";
echo $label; // Adult
?>
Nested if
An if statement inside another if:
<?php
$age = 25;
$hasTicket = true;
if ($age >= 18) {
if ($hasTicket) {
echo "Welcome to the event!";
} else {
echo "You need a ticket.";
}
} else {
echo "Sorry, you must be 18+.";
}
?>
Tip: Deeply nested if statements are hard to read. Consider extracting conditions into variables or using early returns.
The match expression
PHP 8+ introduced match — a strict comparison alternative to switch:
<?php
$day = "Monday";
$result = match($day) {
"Monday" => "Start of the work week",
"Friday" => "Almost weekend!",
"Saturday",
"Sunday" => "Weekend!",
default => "Midweek",
};
echo $result; // Start of the work week
?>
Key differences from switch:
<?php
// match uses strict comparison (===)
// switch uses loose comparison (==)
$value = "1";
match($value) {
1 => "integer one", // NO MATCH — "1" !== 1
default => "default",
};
switch($value) {
case 1: // MATCHES — "1" == 1 is true
echo "integer one";
break;
}
?>
match also supports arrow functions for complex logic:
<?php
$statusCode = 404;
$message = match($statusCode) {
200 => "OK",
301 => "Moved Permanently",
404 => "Not Found",
500 => "Server Error",
default => "Unknown status",
};
echo "$statusCode: $message"; // 404: Not Found
?>
Combined conditions
Use logical operators to combine multiple conditions:
<?php
$age = 25;
$income = 50000;
// AND: both must be true
if ($age >= 18 && $income >= 30000) {
echo "You qualify for the premium card.";
}
// OR: at least one must be true
if ($age < 12 || $age > 65) {
echo "You get a discounted ticket.";
}
// NOT: inverts the condition
if (!isset($_GET["page"])) {
$page = 1;
}
?>
Null coalescing as if-else
Use ?? for null checks:
<?php
$username = $_POST["username"] ?? $_GET["username"] ?? "Anonymous";
// Equivalent to:
if (isset($_POST["username"])) {
$username = $_POST["username"];
} elseif (isset($_GET["username"])) {
$username = $_GET["username"];
} else {
$username = "Anonymous";
}
?>
Best practices
<?php
// GOOD: Early returns reduce nesting
function getDiscount(float $price, bool $isMember): float {
if ($isMember) {
return $price * 0.9;
}
if ($price > 100) {
return $price * 0.95;
}
return $price;
}
// AVOID: Deeply nested if-else
if ($a) {
if ($b) {
if ($c) {
// Hard to follow
}
}
}
?>
Mini Practice
Write PHP code that:
- Uses if-elseif-else to convert a numeric grade (0-100) to a letter grade
- Uses the
matchexpression to map HTTP status codes to messages - Combines conditions with
&&and|| - Uses the ternary operator for a simple conditional assignment
Up Next
In the next lesson, you'll learn about Loops — for, while, do-while, and foreach in PHP.
Related Topics
Frequently Asked Questions about If Else
What is If Else in PHP?
If Else is a fundamental concept in PHP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn If Else?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn If Else.
Why is If Else important in PHP?
If Else is essential for PHP development. Understanding this concept will help you write better code and solve real-world problems more effectively.