Which of the following are true about the foreach loop in PHP?
- The foreach loop is used exclusively for arrays
- The foreach loop can only access the values of an array
- The foreach loop can access both the keys and values of an array
- The foreach loop is not a valid construct in PHP
The correct option is: "The foreach loop can access both the keys and values of an array." In PHP, the foreach loop allows you to iterate over each element in an array and access both the keys and values of the elements. During each iteration, you can use the "key" variable to access the key/index of the current element, and the "value" variable to access the value of the element. This makes the foreach loop a versatile construct for working with arrays. Learn more: https://www.php.net/manual/en/control-structures.foreach.php
Loading...
Related Quiz
- Which of the following statements about variable scope in PHP are correct?
- Which PHP function can be used to check if a function has been defined?
- Which of the following is used in PHP to declare a floating-point number?
- You are writing a PHP script and you want to stop the execution of a loop once a certain condition is met. How would you do this using break?
- What is the purpose of the file_exists() function in PHP?