Sunday, 31 July 2011

PHP - For Loop

PHP - For Loop

The for loop is simply a while loop with a bit more code added to it. The common tasks that are covered by a for loop are:


  • Set a counter variable to some initial value.



  • Check to see if the conditional statement is true.



  • Execute the code within the loop.




  • Increment a counter at the end of each iteration through the loop.


  • The for loop allows you to define these steps in one easy line of code.
    It may seem to have a strange form, so pay close attention to the syntax used!

    For Loop Example

    Let us take the example from the while loop lesson and see how
    it could be done in a for loop. The basic structure of the for loop is as
    follows:


    Pseudo PHP Code:

    for ( initialize a counter; conditional statement; increment a counter){
     do this code;
    }
    

    Notice how all the steps of the loop are taken care of in the for loop
    statement. Each step is separated by a semicolon: initiliaze counter, conditional statement, and the
    counter increment. A semicolon is needed because these are separate expressions. However, notice
    that a semicolon is not needed after the "increment counter" expression.
    Here is the example of the brush prices done with a for loop .

    PHP Code:

    $brush_price = 5; 
    
    echo "<table border=\"1\" align=\"center\">";
    echo "<tr><th>Quantity</th>";
    echo "<th>Price</th></tr>";
    for ( $counter = 10; $counter <= 100; $counter += 10) {
     echo "<tr><td>";
     echo $counter;
     echo "</td><td>";
     echo $brush_price * $counter;
     echo "</td></tr>";
    }
    echo "</table>";
    
    

    Display:

    QuantityPrice
    1050
    20100
    30150
    40200
    50250
    60300
    70350
    80400
    90450
    100500

    It is important to note that both the for loop and while loop
    implementation of the price chart table are both OK at getting the job done. However,
    the for loop is somewhat more compact and would be preferable in this situation. In
    later lessons we will see where the while loop should be used instead of the for loop.

    PHP For Each Loop

    Imagine that you have an associative array that you want to iterate through. PHP provides an easy way to use every element of an array with the Foreach statement.

    In plain english this statement will do the following:
    • For each item in the specified array execute this code.
    While a For Loop and
    While Loop will continue
    until some condition fails, the For Each loop will continue until
    it has gone through every item in the array.

    PHP For Each: Example

    We have an associative array that stores the names of people in our
    company as the keys with the values being their age. We want
    to know how old everyone is at work so we use a Foreach loop to print
    out everyone's name and age.

    PHP Code:

    $employeeAges;
    $employeeAges["Lisa"] = "28";
    $employeeAges["Jack"] = "16";
    $employeeAges["Ryan"] = "35";
    $employeeAges["Rachel"] = "46";
    $employeeAges["Grace"] = "34";
    
    foreach( $employeeAges as $key => $value){
     echo "Name: $key, Age: $value <br />";
    }
    

    Display:

    Name: Lisa, Age: 28

    Name: Jack, Age: 16

    Name: Ryan, Age: 35


    Name: Rachel, Age: 46

    Name: Grace, Age: 34

    The syntax of the foreach statement is a little strange, so let's talk about it some.


    Foreach Syntax: $something as $key => $value

    This crazy statement roughly translates into: For each element of the
    $employeeAges associative array I want to refer to the key as $key and the value as $value.

    The operator "=>" represents the relationship between a key and value. You can imagine that
    the key points => to the value. In our example we named the key $key and the value $value.
    However, it might be easier to think of it as $name and $age. Below our example does this and notice how the output
    is identical because we only changed the variable names that refer to the keys and values.

    PHP Code:

    $employeeAges;
    $employeeAges["Lisa"] = "28";
    $employeeAges["Jack"] = "16";
    $employeeAges["Ryan"] = "35";
    $employeeAges["Rachel"] = "46";
    $employeeAges["Grace"] = "34";
    
    foreach( $employeeAges as $name => $age){
     echo "Name: $name, Age: $age <br />";
    }
    

    Display:

    Name: Lisa, Age: 28

    Name: Jack, Age: 16


    Name: Ryan, Age: 35

    Name: Rachel, Age: 46

    Name: Grace, Age: 34

    No comments:

    Post a Comment