Elseif in PHP
In cases where we need to raise all the possibilities. We need something more to unite more than two conditions. That something else is the elseif, which raises a new condition that is only executed if the previous condition was not true; that is, it is part of the else of the previous condition and, at the same time, it poses a new condition.
Elseif (if not, if...)
Many different conditions: the elseif
We have seen that the condition you will evaluate must be of the Boolean type-which means that you can only answer the question as a true or a false answer.
In cases where we need to raise all the possibilities. We need something more to unite more than two conditions. That "something else" is the elseif, which raises a new condition that is only executed if the previous condition was not true; that is, it is part of the else of the previous condition and, at the same time, it poses a new condition.
The elseif, in the same way as the else, can not be executed on its own, it is a "second part" of a previous simple conditional (it is the continuation of an initial if).
<?php
if($_POST["age"] <18) {
print ("¡Hello child!");
} elseif($_POST["age"] <30){
print("¡Hello young!");
} elseif($_POST["age"] >29){
print("¡Hello adult!");
}
?>
We have created three exclusive conditions (which could be many more). Let's see another example: this time, from a succession of initial if, an elseif, and a final else:
<?php
if($_POST["sex"]=="male") {
print("¡Hello Man!");
} elseif($_POST["sex"]=="female")
{ print("¡Hello Woman!"); } elese {
print("Hello ..."); }
?>
As we can see, the elseif not only closes the previous if, but opens a new and raises a new condition, different, independent of the previous one, but with the peculiarity that it will be evaluated only in the event that the previous condition had resulted be false Otherwise, if the previous condition was true, the program does not even take the trouble to evaluate this second condition ... it jumps directly until after the closing key of the if, without evaluating any other condition.
In this way, many alternatives can be linked that require, to be evaluated, that a previous condition have been false.
A very interesting fact is that successive conditions do not have to evaluate the value of the same variable, which gives us the freedom to evaluate different things in each condition:
<?php
if($_POST["sex"]=="male") {
print("¡Hello Man!");
} elseif($_POST["state"]= "single") {
print ("¡Hello single Woman!");
} elseif($_POST["age"] >70) {
print ("¡Hello grandman!");
}
?>
In this case, as there are two possible responses to the first condition, we could raise a second condition that does not reevaluate the same variable "sex" again, but evaluates something else, in this case, "state". And, then, we will raise another condition with the "age", only in the case that the previous condition had not been executed. It is important to be clear that only in the case that the previous condition has been false, the following one is executed. If one of the conditions is true, the rest of the conditions are no longer executed. In the case of having more than two possibilities that evaluate the value of the same variable, we will see that it is much more practical to apply a different structure, which is that of the switch and the case. Let's see an example:
<?php
if ($dia == "monday"){
print ("¡Happy day of the Moon!");
} elseif ($dia == "tuesday"){
print ("¡Happy day of Mars!");
} elseif ($dia == "wednesday"){
print ("¡Happy Mercury Day!");
} elseif ($dia == "thursday"){
print ("¡Happy Jupiter day!");
} elseif ($dia == "friday"){
print ("¡Happy day of Venus!");
} elseif ($dia == "saturday"){
print ("¡Happy Saturn Day!");
} elseif ($dia == "sunday"){
print ("¡Happy day od the Sun!");
}
?>
In this case, in which we will evaluate a series of possible values of a single variable, it is much shorter and simpler to use a selective structure, such as the switch, which we will learn to use next.
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
Elseif in PHP.
Retrieved Dec 15, 2024, from
https://disenowebakus.net/en/elseif-php