foreach loop | PHP Tutorial

– The foreach loop in php works only with Arrays

– Foreach loop in used to loop through various values within an array

– It can used to loop through indexed as well as associated array

For every single iteration the value of current index is assigned to the $value and can be accessed within the array body with $value variable.

Syntax

For every single iteration the value of current index is assigned to the $value and can be accessed within the array body with $value variable.

Example

In the above example, the languages array stores some languages name. The foreach loop uses this array to get values in language variable one by one. The above loop prints all the language one by one.

Output

php
java
python
javascript

Author: Deepak Kumar

This is Deepak Kumar, founder of True Infosystems and of-course the chief content creator at CodingLessions.info. Started my carrier as a Freelancer and now a top-rated Freelancer at Upwork, delivered over 200 successful projects with highly positive user ratings and counting.