·  PHP – Syntax – Statements – Looping – foreach

 

- Info:

  - foreach statement allows you loop through array keys and values.

 

- Syntax:

  - foreach ($array as $value      ) { code }

  - foreach ($array as $key=>$value) { code }

 

- Example:

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

 

      #Test array. ---------------------------------------------------------------

      $array = array("John"=>1, "Jill"=>5);

   

      #Cycle through values: 15 --------------------------------------------------

      foreach ($array as $value) {

        print($value);

      }

 

      #Cycle through key-value pairs: John1Jill5 ---------------------------------

      $array = array("John"=>1, "Jill"=>5);

      foreach ($array as $key=>$value) {

        print($key);

        print($value);

      }

 

      #Break: 1 ------------------------------------------------------------------

      foreach ($array as $value) {

        print($value);

        break;

      }

      

      #Continue: 5 ---------------------------------------------------------------

      foreach ($array as $value) {

        if($value==1) continue;

        print($value);  

      } 

 

    ?>