#3.2 How IF and ELSEIF work in PHP

We are going to see another sentence of the conditional control structures, the last sentence related to the IF conditional in php programming: the ELSEIF statement.

In this class of the php course I will use the following concepts:

  • Using variables in php
  • Control structures in php
  • IF and ELSE Conditionals

After knowing and mastering the use of IF and ELSE We are presented with the possibility of foreseeing multiple conditions and assumptions, all of them with an exclusion relationship.

Content

  • Multiple conditions in php programming
  • Compare multiple conditions with IF and ELSE or ELSE IF
    • Multiple conditions with IF
    • Multiple conditions with IF and ELSE
    • Multiple conditions with IF and ELSE IF
  • Extra: The order of precedence in multiple conditionals
    • Example of an elseif conditional with precedence:

Multiple conditions in php programming

Using the conditionals IF either IF and ELSE It’s easy to troubleshoot with one or two possible cases, but likewise, when faced with multiple related cases, we must nest IF and ELSE over and over again, complicating the final code.

ELSE IF gives us the possibility of foreseeing an assumption dependent on two conditions: that the condition itself is fulfilled (of the else if) and that the previous ones are not fulfilled. This could be called an exclusion relationship, that is, if and only if the previous IF or ELSEIF in the chain of conditions are not met (its condition is false) the programmed condition may occur.

The best way to mentally visualize the problem is with a real case:

Imagine the typical movie in which they have to defuse a bomb, and in which there are three cables distinguished by their color (blue, red, yellow), only one being the one that defuses the bomb counter. If the protagonist follows the blue cable and sees that it is not related to the counter, then he will have to see if it is the red one, if he studies the red cable and it is not related either, he proceeds to look at the last one, which by obligation must deactivate the bomb. Thus, if in the procedure you stop at one of the cables and have to cut it, the rest would no longer be checked. Therefore all these conditions have an exclusion relationship (one condition excludes the others).

Compare multiple conditions with IF and ELSE or ELSE IF

In the previous example I have exposed a real case where a problem with three assumptions is clearly seen, blue cable or red cable or yellow cable. Very good, now let’s see how we could program it making this case a php exercise:

Let’s imagine that the following information is passed to us to solve the problem:

  1. they pass me a variable $cable_to_cut containing the text color of the wire to be cut.
  2. When you find the cable to cut we must display a message that says: I cut the wire of color XXXX.

Multiple conditions with IF

#php
if ( $wire_to_cut == ‘blue’ ){
echo ‘Short the blue wire’;
}
if ( $wire_to_cut == ‘red’ ){
echo ‘Cut the red wire’;
}
if ( $wire_to_cut == ‘yellow’ ){
echo ‘Cut the yellow wire’;
}
#/php

In this solution limited to the single use of the statement IF I have foreseen the three possible cases, with three conditions each. By using just IF to the code interpreter each of the conditions are unrelated. What does this mean? For the code interpreter, even if the first condition is met, it will still check if the others are also met (unnecessary extra work for Apache).

As we have seen, although the solution is correct, it is not efficient, and moreover, it is more expensive to program than others and more prone to possible errors on our part.

Multiple conditions with IF and ELSE

#php
if ( $cable_to_cut == ‘blue’ ) {
echo ‘Short the blue wire’;
} else {
if ( $wire_to_cut == ‘red’ ){
echo ‘Cut the red wire’;
} else {
echo ‘Cut the yellow wire’;
}
}
#/php

In this solution, I am applying an exclusion relationship, preventing the interpreter from reading unnecessary code, and stopping the execution of the code in the first case of the three that is fulfilled. Finally I have programmed by deletion the worst case in the yellow color with a ELSE.

Multiple conditions with IF and ELSE IF

#php
if ( $cable_to_cut == ‘blue’ ) {
echo ‘Short the blue wire’;
} else if ( $cable_to_cut == ‘red’ ) {
echo ‘Cut the red wire’;
} else {
echo ‘Cut the yellow wire’;
}
#/php

In this last php script I have programmed the most efficient and clear solution of the three. If we look at it from the direct translation from English to Spanish it would be: If the wire to be cut is blue then I cut the blue wire, otherwise if the wire to be cut is red then I cut the red wire, otherwise I cut the yellow wire.

Sentence else if It has allowed me to simplify the code of the if and else nested, with fewer tabs, fewer braces, and exactly the same efficiency.

In this basic example I have only used a else ifbut you could actually place so many related and contiguous else if as you need:

Imagine there are five wires instead of three:

#php
if ( $cable_to_cut == ‘blue’ ) {
echo ‘Short the blue wire’;
} else if ( $cable_to_cut == ‘red’ ) {
echo ‘Cut the red wire’;
} else if ( $cable_to_cut == ‘purple’ ) {
echo ‘Cut the purple wire’;
} else if ( $cable_to_cut == ‘black’ ) {
echo ‘Short the ground wire?’;
} else {
echo ‘Cut the yellow wire’;
}
#/php

As you can see I have accumulated three else if one after the other, thus depending on each other with the exclusion relation that I have introduced to you previously, in this way, the conditions are looked at in the human reading order, that is, from left to right and from top to bottom, and if any one of them is met, the rest are no longer checked.

Extra: The order of precedence in multiple conditionals

Many times, apart from an exclusive relationship, the assumptions of the problems usually have a priority relationship. This relation, well implemented, will avoid serious errors and increase the efficiency of the proposed algorithm. We will always place the conditions from highest to lowest priority.

Example of an elseif conditional with precedence:

A professional looking for a better job asks us to program a small script that solves the problem of looking for a job. He has sent resumes to many companies, but he wants to be the first to answer a possible interview email from them, and also, he does not want to attend unnecessary interviews. He wants us, at the end of the day, to automatically look at what emails he has received, and depending on the offers, we reply to one or the other, and we only reply to the most important company that is interested in his work. If no response has arrived he also wants to be notified.

The ranking of companies according to this gentleman is:

  1. The AlpujarrasSOFT company or the Poodle company (it is not decided by any)
  2. Olive Technologies
  3. Lowcost Corp.
  4. Garbage Solutions.

To solve the problem we must take into account that we will receive the email address in the variable $from_email and we can use the php function strpos() which tells us if one string contains another and at what character it starts:

#php
$es_alpujarras = strpos( $from_email, ‘AlpujarrasSoft’ );
$is_poodle = strpos( $from_email, ‘Poodle’ );
$es_aceituna = strpos( $from_email, ‘Olive Technologies’ );
$es_lowcost = strpos( $from_email, ‘Lowcost’);
$is_garbage = strpos( $from_email, ‘Garbage’);
if ( false != $es_alpujarras || false != $es_poodle ) {
if ( false != $es_alpujarras ){
echo ‘I send email to Alpujarras’;
}
if ( false != $is_poodle ) {
echo ‘Send email to Poodle’;
}
} else if ( false != $is_olive ) {
echo ‘I send email to Aceituna Technologies’;
} else if ( false != $is_lowcost ) {
echo ‘I send email to Lowcost Corp.’;
} else if ( false != $is_gargable ) {
echo ‘I send email to Gargage Solutions’;
} else {
echo ‘Better stay as you are’;
}
#/php

In this much more complex script than the previous ones, I have strictly followed the priority set by the client, and in the case of the first two, both with the same priority, I have established a double condition that allows me to encompass the possibility that one of The two companies, or both, have answered the mail and therefore send the mail back to the ones that are relevant (both or one).

With this code, only the emails with the highest priority will be sent (two in the first case or one in the rest), and the rest will be avoided. Lastly, as a rule-out, I leave the possibility that no one has responded to the CVs.

Leave a Reply