The if statement is used to execute code only when a condition is true. The else block runs when the condition is false.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are not an adult.";
}
?>
👉 Explanation:
if checks the condition and executes the block if true.else executes only when the if condition is false.The elseif statement allows checking multiple conditions one by one.
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 75) {
echo "Grade B";
} elseif ($marks >= 50) {
echo "Grade C";
} else {
echo "Fail";
}
?>
👉 Explanation:
if checks the first condition.elseif checks additional conditions when the above ones are false.else runs when all conditions are false.