Advertisement

Google Ad Slot: content-top

PHP Nested if Statement

~1 min read · PHP
On this page

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
= 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."; } ?>
Try it yourself