Core PHP

Foreach Loops

This PHP code is an example program that demonstrates how to use foreach loops.

ForeachLoops.php

<?php

  // Create an array of the evangelists.
  $saEvangelists = ["Matthew", "Mark", "Luke", "John"];

  // The common syntax for a foreach loop
  foreach($saEvangelists as $sEvangelist) {
    echo $sEvangelist."<br />";
  }

  echo "<hr />";
  // The alternative syntax for a foreach loop
  foreach($saEvangelists as $sEvangelist) :
    echo $sEvangelist."<br />";
  endforeach;

?>
 

Output

 
 

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