Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 12 - Advanced Loops


    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:20 PM

    Now that we have had a little more practice with control structures I want to introduce the last ones to you. They are "Do while", "switch", "while", "elseif", and "for". First lets start with "switch".

    Switch/Case loops:


    Lets say you wanted to find the city someone was from, with a "for" loop your code might look like this:

         $city = "Temecula";
    if ($city == "L.A.") {
    echo "You live in L.A.";
    } else {
    if ($city == "Dallas") {
    echo "You live in Dallas";
    } else {
    if ($city == "New York") {
    echo "You live in New York";
    } else {
    if ($city == "Washington") {
    echo "You live in Washington";
    } else {
    if ($city == "Phoenix") {
    echo "You live in Phoenix";
    } else {
    echo "You live somewhere else...";
    }
    }
    }
    }
    }
    ?>



    However, if you were to use a "switch" or "case" loop you could make your code a little more friendlier:


         $city = "Temecula";
    switch($city) {
    case "L.A.": echo "You live in L.A."; break;
    case "Dallas": echo "You live in Dallas"; break;
    case "New York": echo "You live in New York"; break;
    case "Washington": echo "You live in Washington"; break;
    case "Phoenix": echo "You live in Phoenix"; break;
    default: echo "You live somewhere else...";
    }
    ?>



    As you can see this saves a little more room than an "if" statement. I want you to notice that there is a "break" after each "case" statement. "Break" tells the computer to "stop the code! break out of it!" This keeps PHP from executing each following case statement. These "break" commands are not required, however, without them the case statement will execute every case statement following the "case" statement that is TRUE.


    For example if the code was:


         $city = "Temecula";
    switch($city) {
    case "L.A.": echo "You live in L.A.";
    case "Dallas": echo "You live in Dallas";
    case "Temecula": echo "You live in Temecula";
    case "Washington": echo "You live in Washington";
    case "Phoenix": echo "You live in Phoenix";
    default: echo "You live somewhere else...";
    }
    ?>




    You would see this print out:


    You live in Temecula
    You live in Washington
    You live in Phoenix
    You live somewhere else...


    Now don't get me wrong; this is VERY handy for a lot of things, but in this case we want to "break" out of the switch code so we added "break;". One last thing I want you to remember about switch loops is that you don't put a "case" command before the "default" because there is no case left that is why it is the default.




    For Loops


    There are three parts to a FOR loop: the variable, the condition, and the action. First you define a variable for the loop, then you check that variable against the condition to see if it is TRUE, and last you set an action to happen to the variable at the end of each iteration through the loop. For example:


         for ($x = 0; $x < 10; $x = $x + 1) {
    echo "$x";
    }
    ?>


    This loop will print the value of "$x" while "$x" is less than 10. Then it will ad '1' to the value of "$x" and start over.

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9




    Now look at this loop:



     $text = "Learn PHP";

    for ($var = strlen($text); $var > 1; $var = $var - 1) {
    echo "The value of $var is $var";
    }
    ?>



    First we made a string called "$text" and put the words "Learn PHP" in it. Remember that the first thing you have to do for a "FOR" loop is make a variable that will control the loop, so I made a variable called "$var" and set it equal to the number of characters in the string "$text" (which is nine). Then I said that while the value of "$var" is greater than "1" ($var > 1) subtract "1" from it ($var - 1). This code will output:

    The value of $var is 9
    The value of $var is 8
    The value of $var is 7
    The value of $var is 6
    The value of $var is 5
    The value of $var is 4
    The value of $var is 3
    The value of $var is 2

    The code will run the last line (The value of $var is 2) and then subtract "1" from the value of "$var" - at which point the loop will start over again. However, this time the value of "$var" will now be "1" which is NOT greater than "1" so the loop will terminate.





    While



    A "while" loop is very simple.

         while ($n == TRUE) {
    do this or that...
    }
    ?>


    In human language it can be read as

    "while (the condition) is TRUE do this"

    For example:


         $n = TRUE;
    while ($n == TRUE) {
    echo "$n is TRUE!";
    }
    ?>

    This loop will run forever because it will always equal TRUE. It will just keep printing out "TRUE is TRUE!"


    You can change the condition to anything you want. Just look at these:

     
    $n = 12;
    while ($n <= 13) {
    echo "$n is less than or equal to 13!";
    $n = $n + 1;
    }

    $n = 15;
    while ($n > 13) {
    echo "$n is greater than thirteen!";
    $n = $n - 1;
    }

    $n = 1;
    while ($n != 9) {
    echo "$n is NOT equal to 9!";
    $n++;
    }

    ?>


    if you ran these you would see the following:


    12 is less than or equal to 13!
    13 is less than or equal to 13!
    15 is greater than thirteen!
    14 is greater than thirteen!
    1 is NOT equal to 9!
    2 is NOT equal to 9!
    3 is NOT equal to 9!
    4 is NOT equal to 9!
    5 is NOT equal to 9!
    6 is NOT equal to 9!
    7 is NOT equal to 9!
    8 is NOT equal to 9!


    The first loop will run twice before it equals FALSE. In human language it says:
    make a vaiable named "n" and set it equal to 12
    while "$n" is less than or equal to 13 
    print "(the value of $n) is less than or equal to 13!"
    then add "1" to the value of "$n" and start the loop over.

    The second while loop will also run twice before it equals FALSE. In human language it says:
    make a vaiable named "n" and set it equal to 15
    while "$n" is greater than 13 
    print "(the value of $n) is greater than thirteen!"
    then subtract "1" from the value of "$n" and start the loop over.


    The final while loop will run eight times before it equals FALSE. In human language it says:
    Set the vaiable named "n" to a value of "1"
    while "$n" is NOT greater than nine 
    print "(the value of $n) is NOT equal to 9!"
    then add "1" to the value of "$n" and start the loop over.


    Do / While


    "Do while" loops are like while loops in every way but one - the condition isn't checked to see if it is TRUE until the END of the loop iteration. The following code will run one time before it is checked and found to be FALSE.

         $number = 8;
    do {
    echo "value of $number is $number";
    } while ($number > 10);
    ?>


    this will print:

    value of $number is 8




    Lets make some advanced loops and put what we have learned to work.


    For more practice with LOOPS you can visit these sites:
    Melonfire
    Tizag
    pixelDepth switch