• home
  • forum
  • my
  • kt
  • download
  • Loopy Looping

    Author: 2008-08-23 13:06:50 From:

    One of the key concepts in computer programming is looping or repetition. This is because it allows the user to carry out an endless number of instructions while a certain condition is true. For example on this forum we load a page of posts using a loop to cycle through the data and place it into the template. In this tutorial we will learn a few of the commonly used methods of looping in PHP.

    Loopy Looping!
    Firstly we will learn how to use a for loop. This should not be confused with the foreach loop we'll learn later in this tutorial.

    Code Box
    for ( start ; condition ; change )
    {
        echo ( ' ... ' );
    }

    In the above piece of code you can see the logical structure of the statement. The first value labelled start is where we define the variable that will be used to count through the loop. The condition is the same as an if logic statement. While the condition evaluates to true the loop will repeat over again. Finally the change is what we should do to the variable to make the loop exitable.

    Here is a real example with exampler values.

    Code Box
    for ( $x = 1; $x < 1000; $x = $x + 1 )
    {
        echo ( ' The value of the variable is ' . $x );
    }

    In this example we start the loop with x being set to 1. Each time the loop runs we increase the value of x by one, and the condition, it will repeat while x is less than 1000.

    The example below uses $x++ which is another way of increasing the value of x by 1.

    Code Box
    for ( $x=1; $x<5; $x++ )
    {
        echo ( ' $x is equal to ' . $x . '<br />' );
    };

    This code gives the following output.

    Quote
    $x is equal to 1
    $x is equal to 2
    $x is equal to 3
    $x is equal to 4

    Using a while or do while loop.
    The code below works like this. While the the variable stop is not set it will continue to loop. The variable stop is set only when the number chosen at random is 123.

    Code Box
    while ( !$stop )
    {
        if ( rand ( 100 , 200 ) == 123 ){
            $stop = 1;
        };
    };

    Rather than using variable's to control loops we can use the break statement. This will exit any loop that is currently running. The below code also demonstrates this.

    Code Box
    while (true)
    {
        if ( rand ( 100 , 200 ) == 123 ){
            break;
        };
    };

    The do while loops is slightly different because the loop always runs once whether or not the condition was true the first time around.

    Code Box
    $x = 0;
    do {
        echo ($x . '<br />');
        $x++;    
    } while ( $x < 10 );

    Using the foreach loop!
    The foreach loops is used when arrays are concerned. In these tutorials we havn't covered arrays much yet so we'll keep this example short.

    Code Box
    foreach ( $ArrayName as $K => $V )
    {
        // each element of the array is cycled through. K is the element index, V is the value.
        echo ( '$ArrayName[$K] is equal to $V' );
        echo ( '$ArrayName[' . $K . '] is equal to ' . $V );
    }

    We will come back to foreach loops once we have covered more on arrays.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (17)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (54)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot