Code2Design.com

User login

Programming

The Layout

Navigation

Popular content

Resources

Who's online

There are currently 0 users and 4 guests online.

Lesson 12 - Advanced Loops

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:

<?php
    $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:

<?php
    $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:

<?php
    $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:

<?php
   
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:

<?php
$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.

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

In human language it can be read as

"while (the condition) is TRUE do this"

For example:

<?php
    $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:

<?php
    $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.

<?php
    $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


Submitted by David on November 9, 2006 - 11:51pm.
printer friendly version

So far so good :)

I started teaching myself some php with no prior pragramming knowledge a few years back but got busy with offline stuff and havn't touched it for close to 2 years now so I'm going over some revision.
I just have to say that I wish this tutorial was around when I fist sarted.

Most of the tutorials on php that I could find back then expected you to already know all the common programming jargon and so if you didn't know any of the programming jargon, they were very hard for a non-progarmmer to follow.

This tutorial, and not just this lesson but all lessons 1 to 12 that I've gone over so far, is very nicely done. Simple and easy to follow with the barest of basics explained :)
I love the video tutorials in the earlier lessons and hope to see some more of those here in some of the later lessons.
Your examples are also very practical and easy to follow and the links to other pages for further reading on each particular lesson are a nice added bonus :)

I like the way you show the "human language" translation of code, it really makes it easy to understand.

Many out there may scoff but when I was first trying learn, I had no idea what a variable was, or what a function or a loop was and most of the so-called tutorials I could find at the time simply assumed that I already knew what they were talking about. This fact pretty much made those tutorials almost completely useless. Eventually, after going over hundreds of such mind boggling tutorials and endless hours of research I did finally get to learn some stuff that I would have learned in two seconds flat if any of the writers of those tutorials had been bothered to include simple explainations like you do.

Keep up the good work :)


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati