Advertisement
Google Ad Slot: content-top
PHP Nested if Statement
Nested if statements allow you to place one if...else structure inside another if or else block, creating a more complex decision-making flow. This is useful when you need to check multiple conditions that depend on each other.
Basic Syntax of Nested if
if (condition1) {
// Code to execute if condition1 is true
if (condition2) {
// Code to execute if both condition1 and condition2 are true
} else {
// Code to execute if condition1 is true and condition2 is false
}
} else {
// Code to execute if condition1 is false
}
Example
<?php
$age = 20;
$hasID = true;
if ($age >= 18) {
// Nested if inside the first if
if ($hasID) {
echo "You are allowed entry.";
} else {
echo "You need an ID to enter.";
}
} else {
echo "You are too young to enter.";
}
?>