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
<!DOCTYPE html>
<html>
<body>
<?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.";
}
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.