What does the foreach loop in PHP do?
Correct answer
B. Loops through the elements of an array
14 views
B. Loops through the elements of an array
14 views
The foreach loop iterates over the elements of an array (or any iterable object), giving you each value — and optionally each key — in turn: foreach ($items as $key => $value).
A for loop is the one that runs a fixed number of times. foreach is preferred for arrays because it needs no counter and cannot run past the end.
Practise more Computer Science MCQs with answers and explanations on MCQsExam.com.
Not sure why the answer is B? Ask here and get an explanation.