PHP If...Else Statement
The if...else statement is used to execute a block of code if a condition is true, and another block of code if the condition is false.
Syntax
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Example of PHP If...Else Statement
<?php
$x = 10;
if ($x > 5) {
echo "x is greater than 5";
} else {
echo "x is less than or equal to 5";
}
?>
Output:
x is greater than 5
Explanation: This example demonstrates the use of the if...else statement in PHP. If the condition is true, the code inside the if block is executed. If the condition is false, the code inside the else block is executed.
Nested If...Else Statements
Nested if...else statements can be used to check multiple conditions.
<?php
$x = 10;
if ($x > 5) {
if ($x > 10) {
echo "x is greater than 10";
} else {
echo "x is between 5 and 10";
}
} else {
echo "x is less than or equal to 5";
}
?>
Output:
x is between 5 and 10
Explanation: This example demonstrates the use of nested if...else statements in PHP.