Now: Tutorial for Web and Software Design > PHP > PHP Basic > PHP Content
> Advanced Control Structures [Bookmark it]
Advanced Control Structures
PHP Foundations

Advanced Control Structures

04/19/2001

This article will cover advanced control structures and techniques, including multi-conditional if statements and an introduction to the for statement.

The 'for' loop

In our previous articles, we discussed using a while loop as a means to repeat a block of code until the condition it provided is determined to be false. Although useful, while loops are used primarily when you dont initially know how many times you want to execute the code block. The for statement is another repetition statement designed to be used when executing a code block a specific number of times. The syntax for a for loop is:

For(initialization; condition; increment) {

// Code to loop

}

Unlike the while loop, a for loop contains three parts separated by semicolons. When a for statement executes, the initialization statement is automatically executed. Then, once the initialization statement has finished, the condition provided is evaluated. If this condition is true, the code block enclosed within the for statement is then executed. After the code within the loop has been executed, the increment statement is evaluated and the condition is then re-evaluated. Note that in a for loop, the initialization statement is only evaluated once -- before any code within the loop is evaluated -- but the increment portion is evaluated after each iteration of the loop. This looping process (evaluate condition, evaluate block, evaluate increment statement) is continued until the conditions provided are false and the loop ends (See Figure 1).

Diagram of 'for' loop processing
Figure 1. Processing of a "for" loop.

Now that we have an idea of how a for statement works, let's see it in action by looking at the earlier example we used to demonstrate the workings of a while loop where we wanted to display a count of all the numbers between 1 and 5:

<?php



for($L = 1; $L <= 5; $L++) {



echo $L."<br />";



}

?>

As expected, the output is identical to the while loop and displays the numbers 1 through 5. Again note that the increment statement ($L++) could be any valid math statement. For example, we could count backwards from 5 to 1 by using the following:

<?php



for($L = 5; $L >= 1; $L--) {



echo $L."<br />";



}

?>

[1] [2] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    A Day in the Life of #Apac
    Important Notice for Apach
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial