PHP Nested If
In this page:
if (outer_condition) {
if (inner_condition) {
// runs when both are true
}
}
Basic Nested If Structure
Placing one if statement inside the body of another creates a nested if -- the outer condition acts as a gate, and the inner condition is only checked at all once execution has already entered the outer block.
Example: Basic Nested If Structure
<?php
// Declare `$loggedIn`, set to `true`
$loggedIn = true;
// Check whether `$loggedIn`
if ($loggedIn) {
// Declare `$isAdmin`, set to `true`
$isAdmin = true;
// Check whether `$isAdmin`
if ($isAdmin) {
// Print "Welcome, admin!" to the output
echo "Welcome, admin!";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
Nesting Inside an Else Branch
A nested if can live inside an else block just as easily as inside an if block, letting you handle a completely different set of sub-cases depending on which branch of the outer condition was taken.
Example: Nesting Inside an Else Branch
<?php
// Declare `$loggedIn`, set to `false`
$loggedIn = false;
// Check whether `$loggedIn`
if ($loggedIn) {
// Print "Welcome back!" to the output
echo "Welcome back!";
// Otherwise, run this branch
} else {
// Declare `$hasAccount`, set to `false`
$hasAccount = false;
// Check whether `$hasAccount`
if ($hasAccount) {
// Print "Please log in." to the output
echo "Please log in.";
// Otherwise, run this branch
} else {
// Print "Please create an account." to the output
echo "Please create an account.";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
When to Combine Conditions with && Instead
If an inner if only ever needs to run when BOTH the outer and inner conditions are true -- with no other combination of outcomes mattering -- combining them into a single if with && produces the exact same behavior with less nesting and less indentation.
Example: When to Combine Conditions with && Instead
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasTicket`, set to `true`
$hasTicket = true;
// Check whether `$age >= 18 && $hasTicket`
if ($age >= 18 && $hasTicket) {
// Print "Entry allowed" to the output
echo "Entry allowed";
}
?>
Login to try C/C++/Java/PHP code in the editor
Nested If for Multi-Step Validation
Nested ifs are a natural fit for multi-step validation, where each step should only be checked if the previous step already passed -- like confirming a form field exists before checking its length, since checking the length of a missing field would cause an error.
Example: Nested If for Multi-Step Validation
<?php
// Declare `$field`, set to "hello"
$field = "hello";
// Check whether `isset($field)`
if (isset($field)) {
// Check whether `strlen($field) > 3`
if (strlen($field) > 3) {
// Print "Field is valid." to the output
echo "Field is valid.";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
Readability: Alternatives to Deep Nesting
Beyond two or three levels, nested ifs become genuinely hard to follow -- techniques like early returns (exiting a function as soon as a condition fails), combining conditions with &&, or switching to a match/switch expression can flatten deeply nested logic into something much easier to scan.
Example: Readability: Alternatives to Deep Nesting
<?php
// Define the function `checkAccess` taking `$age`, `$hasTicket`
function checkAccess($age, $hasTicket) {
// Check whether `!$hasTicket`
if (!$hasTicket) {
// Return `"No ticket"` from this function
return "No ticket";
}
// Check whether `$age < 18`
if ($age < 18) {
// Return `"Too young"` from this function
return "Too young";
}
// Return `"Access granted"` from this function
return "Access granted";
}
// Print `checkAccess(20, true)` to the output
echo checkAccess(20, true);
?>
Login to try C/C++/Java/PHP code in the editor
- Nesting if statements many levels deep, producing a hard-to-follow staircase of indentation instead of combining conditions with && where that would be simpler.
- Forgetting that an inner if only ever runs at all if the outer if's condition was true -- a common source of confusion when debugging why a block "never runs".
- Misaligning braces in deeply nested blocks, making it hard to tell which closing brace belongs to which if statement.
- A nested if is an if statement written entirely inside the body of another if (or else) block.
- The inner condition is only ever evaluated if the outer condition was true first.
- Two or more independent conditions that must ALL be true can often be simplified from nested ifs into a single if with &&.
Nested if statements are a basic language feature supported identically in every PHP version.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: