Core PHP

For Loops

A for loop can be used to loop over the a set of indices that typically represent an aritmetic sequence. For-each loops can be used to iterate over the entries of an array. Here, we have an example of each type of loop.

ForLoops.php

<?php
  // For loops
  echo "<b>Counting for-loop:</b><br />";
  for ($i = 0; $i < 10; ++$i) {
    echo $i."  ";
  }
  echo "<br /><br />";

  // Foreach loop
  echo "<b>Foreach loop of over an array of Apostles:</b><br />";
  $qaApostles = Array("Peter", "Andrew", "James the Greater (the son of Zebedee)", "John", "Philip",
    "Bartholomew", "Matthew", "Thomas", "James the Less (the son of Alphaes)", "Simon the Zealot",
    "Judas (Thaddeus or Jude)", "Judas Iscariot");
  foreach($qaApostles as $qApostle) {
    echo $qApostle.",  ";
  }
  echo "<br />";
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.