Else and elseif in PHP

For those cases in which the condition is not true, we have the else. The else can never be executed alone, it is just a complement to an if.

Else and elseif in PHP | Learn PHP | For those cases in which the condition is not true, we have the else. The else can never be executed alone, it is just a complement to an if

What to do if the answer is not true? The else and the elseif

Else (if not)

To the conditionals that we have seen, we can also add them (optionally) to execute another block of code in the case that the evaluated condition has been false.

For those cases in which the condition is not true, we have the else. The else can never be executed alone, it is just a complement to an if.

The else turns the if into a fork between two possible paths, and automatically decides which block of code to execute between two different possible blocks. Two blocks of code subject to the same condition.

The if structure with else is the following:

If (condition to evaluate) {Block that executes only if the condition turns out to be true} else {Block that executes only if the condition is false}

A fundamental condition is that both blocks are never executed on the same occasion. Either the initial block is executed, the one that is wrapped between the keys of the if, or the block wrapped by the else keys, but never both. Or one block, or the other. It is exclusive.

This is so because the condition of the if can not be true and false at the same time, at the same moment. Either it is true, or it is false.

If the condition is true, the block of orders that is enclosed between the first keys, the one of if, and not the block of the else is executed.

If, on the other hand, the condition is false, the if block is not executed, and only the block delimited between the keys of the else is executed. Either one, or the other.

True condition IF

In our previous real life example, it could be like this:

if ($clima == "rain") {
echo "Take umbrellas";
} else {
echo "Do not carry an umbrella";

Of course, for this to work, let's previously give it a value of $ climate, and let's try to change it; Sometimes it is "rain", so we see how the real block is executed, and that sometimes it is something else, so the else block is executed.

For greater clarity, to the blocks of instructions that must be executed - what is inside the keys of the if or the else-, we leave them a tabulator, or 3 or 4 spaces of indentation on their left, to facilitate their reading.

Let's see another real case. We are going to make a code that is "polite", that greets people according to their sex. We create the file Elige.html, which consists of the following form (of course, we add the DTD, html tags, head, body, etc.):

<form action="greet.php" method="post">
<select name="sex">
<option value="male">Male</option>
<option value="female">Female</option>
<input type="submit" value="Submit">

On the other hand, we receive the value of the variable "sex" in the file named hi.php, depending on what the user has chosen, we will show you one or another greeting: Code for greet.php:

if ($_POST["sex"] == "male"){
print ("¡Hello man!");
} else {
print ("¡Hello woman!");

Flow diagram if condition

Let's analyze the first element of a conditional that, as its name implies, is the condition that will be evaluated. Inside the parentheses, next to the word if, we make an affirmation, which will take the form of a comparison, which will only be evaluated as true or false (they are known in programming as Boolean conditions). In this case, what we want to know is whether the current value of a variable (in this example, the variable "sex") is exactly the same as masculine.

That is why we have written if($_POST["sex"]=="masculine") and, having placed a double sign equal, we are only asking, without "assigning" a value to the variable. The double sign equals, is the comparison operator, which allows us to find out if two things are equal.

If we had written $_POST["sex"]="male" (with a single equal sign, which is the assignment operator), every time the if would have been true, since it is a unique equal sign is assigned to the variable $_POST["sex"] the value "male"; therefore, the condition will be interpreted in another way, since it will be evaluated if "the assignment could be made", if the value "masculine" could be saved within $_POST["sex"], and since this does not usually fail, that condition will always be true.

Did you like it or was it useful?

Help us share it in Social Networks

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.


Not finding what you need?

Use our internal search to discover more information
Sponsored content:

Leave your Comment


Your business can also appear here. More information