Home » PHP foreach loop

PHP foreach loop

by Online Tutorials Library

PHP foreach loop

The foreach loop is used to traverse the array elements. It works only on array and object. It will issue an error if you try to use it with the variables of different datatype.

The foreach loop works on elements basis rather than index. It provides an easiest way to iterate the elements of an array.

In foreach loop, we don’t need to increment the value.


There is one more syntax of foreach loop.



php for loop flowchart

Example 1:

PHP program to print array elements using foreach loop.


Summer   Winter   Autumn   Rainy  

Example 2:

PHP program to print associative array elements using foreach loop.


Name : Alex  Email : [email protected]  Age : 21  Gender : Male  

Example 3:

Multi-dimensional array


Alex Bob Camila Denial  

Example 4:

Dynamic array


j a v a t p o i n t  

Next TopicPHP While Loop

You may also like