24 – 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.

An example using the first given syntax:
This code below declares an array of months, then it displays each month using foreach loop.


And example using the second syntax:
The code below is an associative array that stores the Employee name and their Work as the keys with values. And using the foreach loop it prints out the name and work of each employee.





Readers might read also:



Looking for more source code? Type your keyword here here!


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.