Nested If...else Statement


The if....elseif...else statement executes different codes for more than two conditions.

Syntax

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
}

The example below will output "You got Excellent Grade" if the marks is Grater than 80, "You got First Class" if the marks is Grater than 60 and less than equal to 80 and like wise second class,third class and fail :

Example

<?php
$marks = 80;

if ($marks > 80) {
    echo "You got Excellent Grade";
} elseif ($marks >= 60 && $marks <= 80) {
    echo "You got First Class";
} elseif ($marks >= 50 && $marks <= 60) {
    echo "You got Second Class";
} elseif ($marks >= 40 && $marks <= 50) {
    echo "You got Third Class";
}

elseif ($marks < 40) {
    echo "You are Fail in Exam";
} else {
    echo "You are Absentn in Exam";
}
?>

Run Example>>