The if...elseif...else statement in PHP allows checking multiple conditions in sequence. Only the first true condition executes, and the remaining conditions are skipped.
// PHP if-elseif-else syntax structure
<?php
if (condition1) {
// Executes when condition1 is true
} elseif (condition2) {
// Executes when condition2 is true
} else {
// Executes when all conditions are false
}
?>
// Determine grade based on marks
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade: A";
} elseif ($marks >= 75) {
echo "Grade: B";
} elseif ($marks >= 60) {
echo "Grade: C";
} else {
echo "Grade: F";
}
?>
Grade: B
The condition $marks >= 75 evaluates to true, so PHP prints Grade: B and ignores the remaining conditions.
Adjust the marks to see how PHP evaluates each condition sequentially.
This logic works like a vertical decision ladder where PHP stops at the first true condition and exits the chain.
elseif instead of multiple if blockselse for fallback handlinggrade_check.php