24 – PHP Foreach Loop

0
569

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.

Output:

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.

 

Output:

 

 

Readers might read also:

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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