Updated March 23, 2023
Introduction to elseif in PHP
In this article, we will discuss on elseif in PHP. Like all the programming languages, PHP also provides conditional operators. Using conditional operators, you can provide specific conditions and check for the results accordingly. We have the if, elseif, else and also switch statements that help in having decision-based code on various different conditions. These help in creating test conditions that are in the form of expressions and which can evaluate to either true or false. Let us have a look at elseif statement in detail.
Syntax of elseif Statement
The elseif statement can be used with the if-else statements. The syntax for these statements is as below:
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
elseif cannot be used with if .. else. There has to be a corresponding if-else for any else if to be used in PHP. elseif is used when there is more than one condition to be satisfied in the code. The above syntax shows that if checks for a condition. If the if condition is true the statements following the if will be executed. This means that the other blocks of elseif and else will not be executed. If the condition is not satisfied, then the interpreter will move to the elseif block. This means that the statements in elseif block will be executed. The else block will not be executed in this case. On similar lines, if the if and elseif conditions are not satisfied then the else block will be executed.
Flowchart for elseif in PHP
To understand this better let us have a look at the flowchart for elseif.
Explanation: The flowchart above explains the elseif loop. Once the program starts, it evaluations the ‘if’ condition. The condition is specified in the decision box as it helps in making the decision. If the condition here satisfies then it will go to the conditional code that follows. If this condition is not true, then it will take the second decision box. This decision box is the ‘elseif’ block. The elseif will also have a condition that will help in satisfied a particular condition. The elseif condition if satisfied will execute the statements which are meant for elseif block and the program will terminate. Now, if the elseif condition also does not get satisfied then the else block will be executed. That means when if and elseif’s conditions do not meet it comes to the else block. Whatever statements are specified in else block will be executed and the program will be terminated. The elseif block helps in specifying multiple conditions. There can be multiple elseif conditions in a PHP program.
How does elseif work in PHP?
The elseif statement is a conditional statement which if satisfied performs some task in the code. The interpreter looks for code in ‘if’. If this condition is not matched then it moved on to the elseif block. It is a combination of if and else. It extends an if statement which executes single condition while else if can check for multiple if conditions. Elseif cannot do anything if this condition is false. It will move to the next elseif and if there is not elseif which follows it will look for the next else block available. The conditions present in the else block will be executed if all elseif conditions are found to be false.
Examples of elseif in PHP
Following are the various examples of elseif in PHP.
Example #1
Let’s check for some examples which will help us understand elseif better.
Code:
<?php
$emp = "Rakesh";
if($emp == "Ms. Snehal"){
echo "Hello Ma'am";
} elseif($emp == "Rakesh"){
echo "Good Morning Sir!";
}else {
echo "Morning";
}
?>
Output:
Code Explanation: Here if you check you will find three conditions. If the name of the employee is Snehal then the output should be ‘Hello Ma’am’. If the employee name is Rakesh, then the output should be ‘Good Morning Sir’ while if it is not specified then the default message that should be displayed is ‘Morning’. Here the emp variable has a value defined as Rakesh and the condition for Rakesh instructs the program to display a message: ‘Good Morning Sir’. If you observe the output of this code, you will find it to be ‘Good Morning Sir’.
Example #2
Code:
<?php
$a=5;
$b=5;
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
Output:
Code Explanation: In this example, we have two variables defined as a and b. Both have values as 5. The code here is checking for greater than, smaller than or equal to values and the if condition is checking if a is greater than b. If this is the case, then the output is “a is bigger than b”. If the variables have equal values, then the output expected is “a is equal to b”. This is the elseif condition. The else specifies than if both preceding conditions are not true that infers that a is smaller than b. In our case, the value of both variables is 5, which satisfies the elseif condition. Hence the output of this code is “a is equal to b”.
Conclusion
PHP provides conditional statements like other programming languages. The if, elseif and else help in checking multiple conditions. If helps in checking only one condition while elseif helps the user in checking multiple conditions. This loop can be easily be implemented in PHP and different conditions can be validated. As a result, elseif is a very good way of replacing the ternary operator and switch statements. It is easy to use by beginners and efficient.
Recommended Articles
This is a guide to elseif in PHP. Here we discuss the introduction, syntax, flowchart, and working of elseif in PHP along with examples and code implementation. You may also look at the following articles to learn more –