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
Loading...
Related Quiz
- Is it possible to destroy a cookie?
- In a PHP do...while loop, the condition is tested ______ the code block is executed.
- The round() function in PHP rounds a floating point number to the nearest ______.
- The min() function in PHP returns the ______ value from a list of numbers.
- In PHP, you can upload a file using an HTML form and the POST method.