What the heck is a elseif statement someone help! 3.control flow:switch php


#1

how am i supposed to create an else if statement in php
please help would be appreciated..tnx :smile:


#2

It's just another possibility, on the same level of your first 'if'.
Let's say I only like bananas (++) and strawberries (+) among fruits :

var dessert = "banana";

function fruitsBasket(dessert) = {
if (dessert == "strawberry){
print("Cool! I like it!");
} else if (dessert == "banana"){
print("OMG OMG OMG I LOVE IT");
} else {
print("Meh, no thx");
}
}

fruitsBasket(dessert);


#3

This is JavaScript, not PHP.


#4

Let's look at the code we built in an earlier lesson (#4 in if else module):

 <?php
        $guess = 7;
        $number = 7;
        
        // Write your if/elseif/else statement here!
        if ($guess < $number) {
            echo "Too low!";
        } elseif ($guess > $number) {
            echo "Too high!";
        } else {
            echo "You win!";
        }
      ?>

The above is a three-way conditional branch, as opposed to a normal two-way branch using else. elseif lets us introduce additional conditionals, and therefore more ways to direct control flow. What's more, we are not confined to just the above, we can have multiple elseif conditionals in one statement.

Typically, we would seek out a better alternative to having a large number of elseif's which is where the switch comes in. This allows us to create a more structured, easier to read and debug multi-way branch.

The example in Lesson 1 of this module (Switch) repeats the multi-way example from earlier, except this time with a scene from Goldilocks and the Three Bears...

<?php
    $a = "warm";
    if ($a=="cold") {
        echo "too cold";
    } elseif ( $a=="hot") {
        echo "too hot";
    } else {
        echo "just right";
    }
 ?>

Example of the above written as a switch statement:

<?php
    switch ($a) {
    case "cold": echo "too cold"; break;
    case "hot": echo "too hot"; break;
    default: echo "just right";
    }
?>

#5

Dear mtf,
In (#4 in if else module) we didnt built that code!
there isn't any "elseif" statement there!


#6

Yes, I see what you are saying. The lesson must have been altered since I completed it a couple years ago. There is no elseif asked for though it is written in the comment in the editor window.

The instructions now read,

On line 8, write an if / else statement, just like we did in the last exercise.
If your condition is true, your code should echo "The condition is true"
Otherwise (else) when it is false, your code should echo "The condition is false".
Make sure your condition evaluates to false, so that your program prints out "The condition is false".

I followed the instructions, adding the code above what I already had, and since the output includes the requested line, it still passes.

    <p>
      <?php
        // Write your if/elseif/else statement here!
        if ('gray' == 'white'){
            echo "The condition is true";
        } else {
            echo "The condition is false";
        }
      ?>      
    </p>
    <p>
      <?php
        $guess = 7;
        $number = 7;        
        // Write your if/elseif/else statement here!
        if ($guess < $number) {
            echo "Too low!";
        } elseif ($guess > $number) {
            echo "Too high!";
        } else {
            echo "You win!";
        }
      ?>
    </p>