You are writing a PHP script and you have an array. You want to execute a block of code for each element in the array. How would you do this using a foreach loop?

  • Use the "for" loop
  • Use the "while" loop
  • Use the "do...while" loop
  • Use the "foreach" loop
The correct option is: "Use the 'foreach' loop." In PHP, you can use the foreach loop to iterate over each element in an array. The loop automatically assigns the current element's value to a specified variable, allowing you to perform operations on each element individually. This loop construct simplifies the process of working with arrays and is ideal for situations where you need to process each element without explicitly managing the iteration counter. Learn more: https://www.php.net/manual/en/control-structures.foreach.php
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *