In this tutorial, we will learn about else-if statements in PHP. We will cover the basics of conditional execution using if-else-if statements.
An else-if statement is a conditional statement that allows multiple conditions to be tested sequentially. It provides a way to execute different code blocks based on different conditions.
The syntax for the else-if statement in PHP is:
if (condition1) {
// Code block to execute if condition1 is true
} elseif (condition2) {
// Code block to execute if condition2 is true
} else {
// Code block to execute if none of the conditions are true
}
The else-if statement evaluates the specified conditions in order. The first condition that is true will have its code block executed; if none of the conditions are true, the code block inside the else statement is executed.
$num
.$num
.$num
is positive, negative, or zero.$num
is positive, negative, or zero.<?php
$num = -5;
if ($num > 0) {
echo "$num is positive.";
} elseif ($num < 0) {
echo "$num is negative.";
} else {
echo "$num is zero.";
}
?>
-5 is negative.
$marks
.$marks
.$marks
.<?php
$marks = 85;
if ($marks >= 90) {
echo "Grade: A";
} elseif ($marks >= 80) {
echo "Grade: B";
} elseif ($marks >= 70) {
echo "Grade: C";
} elseif ($marks >= 60) {
echo "Grade: D";
} else {
echo "Grade: F";
}
?>
Grade: B
$temperature
.$temperature
.$temperature
.<?php
$temperature = 75.5;
if ($temperature > 100) {
echo "It's extremely hot.";
} elseif ($temperature > 85) {
echo "It's hot.";
} elseif ($temperature > 60) {
echo "It's warm.";
} elseif ($temperature > 32) {
echo "It's cold.";
} else {
echo "It's freezing.";
}
?>
It's warm.