Mastering the elseif Statement in PHP: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-18 09:13:25   5466  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

The if...elseif...else a special statement that is used to combine multiple if...else statements.

Syntax:



if(condition1){
    // Code to be executed if condition1 is true
} elseif(condition2){
    // Code to be executed if the condition1 is false and condition2 is true
} else{
    // Code to be executed if both condition1 and condition2 are false
}


elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false.

The following example display 'x is greater than y', 'x is equal to y' or 'x is smaller than y' depends on the value of $x or $y.

Example:



<?php

$x = 13;
$y = 13;

if($x > $y){
	echo "x is bigger than y"; 
}elseif($x == $y){
	echo "x is equal to y";
}else{
	echo "x is smaller than y";
}

?>


Output:

If you will rum the above code, you will get the following output.



x is equal to y