1. Home
  2. Computer Science MCQs
  3. PHP
  4. What does the foreach loop in PHP do?

Added 23 Jan 2024

What does the foreach loop in PHP do?

  1. Loops through a block of code a specified number of times
  2. Loops through the elements of an array Correct answer
  3. Loops through the characters of a string
  4. Loops through a database result set

Correct answer

B. Loops through the elements of an array

14 views

MCQSEXAM

Explanation

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.

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More PHP MCQs

Practise all PHP MCQs

Similar MCQs