Categories:

    Lesson 8 - Control Structures 2

    else and elseif

    Now that you understand IF lets look at two other control structures that are used in an IF statement - else and elseif. (The good thing about PHP is that you can kind of guess what each function or control structure does just by looking at its name). Lets start with else.

    Quote:
    Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is NOT met. This is what else is for. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. - php.net

    Taking our previous lesson's code we could change it to:

    If ($your_number > $cpu_number) {
    // "if" $your_number is greater than $cpu_number do the following:
    echo "Your Number Is Bigger!";
    } else {
    // "if" $your_number is less than $cpu_number do the following:
    echo "Your Number Is Smaller!";
    }
    ?>

    Here is a logic example:

    if (expression = true) {
    then do statement
    } else {//If the "if" above is FALSE
    then do this statement
    }
    ?>

    Read the following simple if and else statements outloud and you will be able to hear the logic and understand it better.

    If (1 > 2) {
    echo
    "1 is bigger than 2!";
    } else {
    echo
    "1 is NOT bigger than 2!"; //This would print
    }

    //Human Version:

    If (one is greater than two) {
    Then print this to the screen "1 is bigger than 2!";
    } else {
    //I guess one is smaller than two so...
    Then print this to the screen "1 is NOT bigger than 2!"; //This would print
    }


    // Using the FALSE value

    If (FALSE) {
    echo
    "The if is FALSE!";
    } else {
    echo
    "So the else is TRUE!"; //This would print
    }

    // Using the TRUE value
    If (TRUE) {
    echo
    "The if is TRUE!"; //This would print
    } else {
    echo
    "So the else is FALSE!";
    }
    ?>

    elseif

    Quote:
    elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. - php.net

    Taking lesson seven's previous code we could change it to:

    /* Make a new variable called "$cpu_number"
    and set it equal to a random number between 0 and 100. */
    $cpu_number = rand(0, 100);

    // Make a new variable called "$your_number" and set it equal to "50".
    $your_number = 50;

    If (
    $your_number == $cpu_number) {

    //"if" $your_number is equal to $cpu_number do the following:
    echo "Your number the same!";

    } elseif (
    $your_number > $cpu_number) {

    /* "if" $your_number is greater than $cpu_number do the following: */
    echo "Your Number Is Bigger! CPU number is $cpu_number";

    } else {

    //The only option left is ($your_number < $cpu_number) So....
    // "if" $your_number is less than $cpu_number do the following:
    echo "Your Number Is Smaller! CPU number is $cpu_number";

    }
    ?>

    Logic example:

    if (expression = true) {
    then do statement
    } elseif (this expression = true) {
    then do this statement
    } else {//"if" everything else is FALSE
    then do this statement
    }
    ?>
    Quote:
    There may be several elseif's within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

    The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE. - php.net

    So as you can see, the else and elseif are very useful in keeping extra IF's out of your code. Take the following as an example:

    If (1 > 2) {
    echo
    "1 is equal to 2!";
    }
    If (
    2 >= 1) {
    echo
    "2 is greater than or equal to 1!";
    }
    If (
    2 == 2) {
    echo
    "2 is equal to 2!";
    }
    /////////////////
    // Using ELSE
    /////////////////
    If (1 > 2) {
    echo
    "1 is equal to 2!";
    } elseif (
    2 >= 1) {
    echo
    "2 is greater than or equal to 1!";
    //the IF ends here because this is true.
    } elseif (2 == 2) {
    //this is also true but the loop ended when it found the true value above!
    echo "2 is equal to 2!";

    }
    ?>

    When you start running multiple IF's in your code you leave it open to bugs because you take a chance on having a weird statement that could be true in more than one of the [/i]IF[/i]'s. (like the code above). So by placing your code in a looooooong IF made up of else's and elseif's you can make sure that only one of the statements will be true and after that statement is run - the rest of the statement (the other else's and elseif's) will be skipped. (this is assuming you only want one statement to run.)

    Here are some more examples of control structures to help you better understand them:

    $fruit = 'apple';
    if (
    $fruit == 'pear') {
    echo
    "$fruit is a pear!";
    }
    elseif (
    $fruit == 'banana') {
    echo
    "$fruit is a banana!";
    }
    else {
    echo
    "then $fruit must be an apple because it is not a banana or pear!";
    }
    ?>

    This would output the last 'else' statement because the first two Control Structures were FALSE. ($fruit = 'apple', not 'banana' or 'pear'.)

    0 Responses