PHP Tutorial [ 4 ]

Discussion in 'PHP' started by DarkMindZ, Dec 31, 2007.

  1. #1
    Welcome to Tutorial #4 in my PHP sega.

    In this tutorial you will learn all about looping, how they work, and how to use them effectivly and properly.

    - While -
    =-=-=-=-=
    The while function will always execute the code inside of it as long as it remains true.

    Example:


    //set up our counting variable
    $x = 0;
    while($x <= 10){
    echo "Our While Function Is Looping Through The Number: $x
    ";
    //Add a value of 1 to the variable $x
    $x++;
    }
    ?>

    This should produce something like this:

    Our While Function Is Looping Through The Number: 0
    Our While Function Is Looping Through The Number: 1
    Our While Function Is Looping Through The Number: 2
    Our While Function Is Looping Through The Number: 3
    Our While Function Is Looping Through The Number: 4
    Our While Function Is Looping Through The Number: 5
    Our While Function Is Looping Through The Number: 6
    Our While Function Is Looping Through The Number: 7
    Our While Function Is Looping Through The Number: 8
    Our While Function Is Looping Through The Number: 9
    Our While Function Is Looping Through The Number: 10


    So as you can see it does it 11 times, this is because PHP parses 0 as a number so we go from 0 to 10.
    Now the way I represented it wouldnt be very useful but it could come in handy for other things. Trust me i've needed it in the past.

    - FOR -
    =-=-=-=

    The For Function is used for the times when you know how many times you wish to execute a block of code.

    Example:

    for($i=0; $i <= 10; $i++;){
    echo "Our For Function Is Looping Through The Number: $i";
    }
    ?>

    This Again should produce something like this:

    Our For Function Is Looping Through The Number: 0
    Our For Function Is Looping Through The Number: 1
    Our For Function Is Looping Through The Number: 2
    Our For Function Is Looping Through The Number: 3
    Our For Function Is Looping Through The Number: 4
    Our For Function Is Looping Through The Number: 5
    Our For Function Is Looping Through The Number: 6
    Our For Function Is Looping Through The Number: 7
    Our For Function Is Looping Through The Number: 8
    Our For Function Is Looping Through The Number: 9
    Our For Function Is Looping Through The Number: 10

    Simple, Short and Sweet, 3 lines of PHP, 1 line less than our while function.

    - Do While -
    =-=-=-=-=-=-=

    The 'Do While' Function loops through a block of code once, and then repeats the loop as long as the specified condition is true.

    Example:

    $x = 0;
    do{
    echo "Our Do While Function is Looping Through The Number: $x";
    }
    while($x <= 10);
    ?>

    That will produce something like this:

    Our Do While Function is Looping Through The Number: 0
    Our Do While Function is Looping Through The Number: 0
    Our Do While Function is Looping Through The Number: 1
    Our Do While Function is Looping Through The Number: 2
    Our Do While Function is Looping Through The Number: 3
    Our Do While Function is Looping Through The Number: 4
    Our Do While Function is Looping Through The Number: 5
    Our Do While Function is Looping Through The Number: 6
    Our Do While Function is Looping Through The Number: 7
    Our Do While Function is Looping Through The Number: 8
    Our Do While Function is Looping Through The Number: 9
    Our Do While Function is Looping Through The Number: 10

    The while function is mostly used for file managers as it saves a lot of time writing everything out, and saves you anticipating what files are in the current dir etc...

    - ForEach -
    =-=-=-=-=-=
    The Foreach Function Loops Through Each Element of an array and executes a block of code.

    Example:

    echo "The Admins Of DarkMindZ.com are:
    ";
    $names = array("RoMeO", "The Reaper", "Ac1d", "Ingelo");
    foreach($names as $name){
    echo $name;
    echo "
    ";
    }
    ?>

    That would output:

    The Admins Of DarkMindZ.com are:
    RoMeO
    The Reaper
    Ac1d
    Ingelo

    As you can see its useful to save space and not having to repeat the same sentance over and over.
    Foreach is mostly used for Mysql Data Handling.

    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


    Well That About Wraps this Tutorial Up.
    I hope you look out For Other tutorials, articles and me personally on the website.

    Enjoy.

    Nusquam Est Impossible.
    Ac1d
     
    DarkMindZ, Dec 31, 2007 IP