Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 8 - Control Structures 2


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:16 PM

    else - elseif - while


    Now that you understand "IF" lets look at some other control structures - "else", "elseif", and "while". (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".


    "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 "if.php" 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!";
    }
    ?>



    Logic example:


     if (expression = true)
    then do statement
    else //"if" FALSE
    then do this statement
    ?>












    "elseif"


    "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 our previous "if.php" 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!";
    } elseif ($your_number == $cpu_number) {
    /* "if" $your_number is equal to $cpu_number do the following: */
    echo "Your Number Is Equal to the CPU's!";
    } else {
    /* "if" $your_number is less than $cpu_number do the following: */
    echo "Your Number Is Smaller!";
    }

    ?>



    Logic example:


     if (expression = true)
    then do statement
    elseif (expression = true)
    then do this statement
    else //"if" everything else is FALSE
    then do this statement
    ?>




    "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.











    "while"


    "while loops are the simplest type of loop in PHP. They behave just like their C counterparts.

    The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once." - php.net.

    Here is an example of using a "while" loop:


     $number = 0;

    while ($number < 10) {
    /* "while" $number is less than o 10 (< 10) - Run the loop! */
    $number++;
    /* add "1" to the number */
    }
    echo $number; //prints "10" to the screen!

    ?>



    Now lets review the code. before the "while" loop starts we set the variable "$number" equal to ZERO. Then the while loop checks to see if "$number" is less than "10". If "$number" IS less than ten it runs the loop and adds "1" to the number, at this point "$number" now equals "1" and the loop starts over.

    Since "1" is still less than ten it will run the loop again and add another "1" to "$number". Now "$number" is equal to "2" and the loop starts over.

    Since "2" is still less than ten it will run the loop again and add another "1" to "$number"! Now "$number" is equal to "3" and the loop starts over again! etc, etc. until the variable "$number" is equal to "10" and equates to FALSE in the start of the while loop (10 < 10 = FALSE). At that point the while loop is over and the next part of the code comes into play - outputting the value of "$number", which is ten.







    Here are some more examples of control structures (loops) 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'.)







     $number = '1';
    while ($number <= 100) {
    echo $number. "";
    $number++;
    }
    ?>





    This would output the numbers from 1 to 100! While "$number" is less than or equal to 100 ($number <= 100) - print it and add one to it. (I put a "" (HTML Line Break) in there just to put each number on a different line.)









    For more information please read these articles:

    PHP: Conditionals

    PHP Looping

    Looping The Loop

    the While Loop

    Loops

    If you find a broken link please PM me, thanks!