Skip to main content

PHP Foreach Loop

Using PHP programming, one of the most commonly used loop to iterate over an array is a foreach loop. The foreach loop works on arrays and objects, and it will give you an error when you try to use it on a variable with a different data type or an undefined variable. Here’s the two sample syntax: The syntax in plain English this statement will do the following, For each item in the specified array execute the statement.

foreach (array_expression as $value)

foreach (array_expression as $key => $value)

An example using the first given syntax:

Add new comment