:::

10. Loops + Arrays = ForEach

<?php
  $numbers = array(1, 2, 3);

  foreach($numbers as $item) {
      echo $item;
  }
?>

 

we use the foreach keyword to start the loop, followed by parentheses. (This is very similar to what we've done with for loops.)

Between the parentheses, we use the$numbers as $item) syntax to tell PHP: "For each thing in $numbers, assign that thing temporarily to the variable$item." (We don't have to use the name $item—just as with for loops, we can call our temporary variable anything we want.)


:::

語系選擇