PHP Nested If Statement
Nested if statements are used to check multiple conditions.
Syntax
if (condition1) {
if (condition2) {
// code to be executed
} else {
// code to be executed
}
} else {
// code to be executed
}
Example of PHP Nested If Statement
<?php
$x = 10;
$y = 5;
if ($x > 5) {
if ($y > 5) {
echo "x and y are greater than 5";
} else {
echo "x is greater than 5, but y is less than or equal to 5";
}
} else {
echo "x is less than or equal to 5";
}
?>
Output:
x is greater than 5, but y is less than or equal to 5
Explanation: This example demonstrates the use of nested if statements in PHP. If the first condition is true, the second condition is checked. If the second condition is true, the code inside the second if block is executed. If the second condition is false, the code inside the second else block is executed.