PHP if/else/elseif Statements
last modified April 16, 2025
The PHP if
, else
, and elseif
statements
are fundamental for controlling program flow. They allow executing different
code blocks based on conditions. These constructs form the basis of decision
making in PHP.
Basic Definitions
The if
statement executes a block of code if a condition is true.
The else
statement executes code when the if condition is false.
The elseif
statement checks another condition if previous ones
were false. Multiple elseif statements can test several conditions in sequence.
Syntax: if (condition) { code } elseif (condition) { code } else { code }
.
Conditions are expressions that evaluate to boolean true or false.
Basic if Statement
This example demonstrates a simple if statement checking a numeric value.
<?php declare(strict_types=1); $age = 20; if ($age >= 18) { echo "You are an adult."; }
The code checks if the $age
variable is 18 or more. If true, it
prints the message. The condition uses the >= comparison operator. Only one
statement executes when the condition is met.
if-else Statement
This example shows how to use if with else for alternative execution paths.
<?php declare(strict_types=1); $temperature = 25; if ($temperature > 30) { echo "It's hot outside."; } else { echo "It's not too hot."; }
The code checks if temperature exceeds 30 degrees. If not, the else block executes. This provides a default action when the condition fails. Only one of the two blocks will ever execute.
Multiple elseif Statements
This example demonstrates checking multiple conditions with elseif.
<?php declare(strict_types=1); $grade = 85; if ($grade >= 90) { echo "Grade: A"; } elseif ($grade >= 80) { echo "Grade: B"; } elseif ($grade >= 70) { echo "Grade: C"; } else { echo "Grade: F"; }
The code checks the grade against multiple thresholds. It stops at the first true condition. The else provides a default for grades below 70. This pattern is common for grading systems.
Nested if Statements
This example shows how to nest if statements for complex conditions.
<?php declare(strict_types=1); $age = 25; $hasLicense = true; if ($age >= 18) { if ($hasLicense) { echo "You can drive."; } else { echo "You need a license."; } } else { echo "You're too young to drive."; }
The outer if checks age, while the inner one checks license status. This creates a hierarchical decision structure. Each condition must be true for the innermost block to execute.
Logical Operators in Conditions
This example demonstrates using logical operators (AND, OR) in conditions.
<?php declare(strict_types=1); $isMember = true; $orderTotal = 120; if ($isMember && $orderTotal > 100) { echo "You qualify for free shipping!"; } elseif ($isMember || $orderTotal > 150) { echo "You get 10% discount."; } else { echo "No special offers available."; }
The code checks combinations of conditions using && (AND) and || (OR). The first condition requires both to be true. The second needs either one true. Logical operators allow complex condition combinations.
Ternary Operator Alternative
This example shows the ternary operator as a concise if-else alternative.
<?php declare(strict_types=1); $isLoggedIn = true; $message = $isLoggedIn ? "Welcome back!" : "Please log in."; echo $message;
The ternary operator evaluates the condition before the ?. If true, it returns the first expression, otherwise the second. This is useful for simple decisions. It's more compact than full if-else blocks.
Checking Array Elements
This example demonstrates using if with array element checks.
<?php declare(strict_types=1); $user = [ 'name' => 'John', 'age' => 25, 'active' => true ]; if (!empty($user['name']) && $user['active']) { echo "Welcome, {$user['name']}!"; } elseif (empty($user['name'])) { echo "Please set your name."; } else { echo "Account not active."; }
The code checks multiple array elements in conditions. It first verifies the
name exists and account is active. The empty
function checks for
non-empty values. Array conditions work like variable conditions.
Best Practices
- Readability: Use clear conditions and proper indentation.
- Simplicity: Avoid deeply nested if statements when possible.
- Comparison: Use strict comparison (===) when type matters.
- Ordering: Place most likely conditions first for efficiency.
- Comments: Document complex conditions for clarity.
Source
This tutorial covered PHP conditional statements with practical examples showing if, else, and elseif usage in various scenarios.
Author
List all PHP basics tutorials.