PHP Looping (Cont.)


The for Statement (Cont.)
The for statement has three parameters: If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

The foreach Statement
The foreach statement is used to loop through arrays.
foreach ( array as value ) {
  code to be executed;  }

For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one)—so on the next loop, you’ll be looking at the next element.
 <html><body>
 <?php
  $class = array( "Freshman", "Sophomore", 
    "Junior", "Senior" );
  $i = 1;
  foreach ( $class as $name )
   if (  == $i++ )
    echo $name;
 ?>
 </body></html>
Output =    

The array([mixed $...]) creates an array and returns an array of the parameters. The parameters can be given an index with the => operator, whose syntax index => values defines index and values, e.g.,
  $class = array( 0 => "Freshman", 1 => "Sophomore",
                  2 => "Junior",   3 => "Senior" );