Javatpoint Logo
Javatpoint Logo

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.

Syntax

There is one more syntax of foreach loop.

Syntax

Flowchart

php for loop flowchart

Example 1:

PHP program to print array elements using foreach loop.

Output:

Summer 
Winter 
Autumn 
Rainy

Example 2:

PHP program to print associative array elements using foreach loop.

Output:

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

Example 3:

Multi-dimensional array

Output:

Alex Bob Camila Denial

Example 4:

Dynamic array

Output:

j a v a t p o i n t

Next TopicPHP While Loop





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA