For... While... ForEach...

Basic or Standard Looping commands are as follows:


For ...


  for ($day = 1; $day <= 31; $day ++) {

    echo '< option value="',

           $day,

           '"> ',

           $day,

           '</option>',

           '\n;'

  }



While ...


  while ( $year <= 2000) {

         echo '< option value="',

                 $year,

                 '">',

                 $year,

                 '</option> \n;'


                $year ++;

   }



ForEach ...


  This command or feature is only available for PHP version 4.0 and Up ...


  syntax: 


foreach (array_expression as $value) < statements > 

   this form loops over the array given by array_expression, on each loop, the value

   of the current element is assigned to $value and the internal pointer is advanced by one(1)

foreach (array_expression as $key => $value) < statements > 

   similar to the 1st form and the current key will be assign to the variable $key


  ex.: 


  < ?php

   $years = range(2000, 2010);

   echo '< select name="Taon">';

   foreach($year as $key => $values) {

         echo '< option value="',

                $key,

                '">',

                $values,

                '</option>;'

   }

   echo '</select>';

  ?>


  • $key is the return value
  • $values is the display value
  • < option> statement requires the ue of double quote
  • range key word creates a array which was used to contain the values from 2000 to 2010