You have an array in your PHP script and you want to execute a block of code for each element in the array. Which type of loop would you use and why?

  • foreach loop
  • for loop
  • while loop
  • do-while loop
If you want to execute a block of code for each element in an array, you would use a foreach loop in PHP. The foreach loop specifically allows you to iterate over the elements of an array without explicitly managing the index or the length of the array. It simplifies the process of accessing each element of the array one by one. The foreach loop automatically traverses the entire array, executing the code block for each element. It provides a convenient and efficient way to work with arrays in PHP and is suitable for scenarios where you want to perform operations on each element of an array individually. 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 *