How does foreach work? | SoloLearn: Learn to code for FREE!

0

How does foreach work?

3/22/2017 11:00:13 AM

Patrick Star

1 Answer

New Answer

+1

foreach loop work with collections without bothering about index of the element. It is best when elements in the collection have to be processed sequentially. foreach (item in collection ) { //to do } example <?php arr = {1,2,3,4,5,6,7,8,9} foreach (n in arr ) { echo n . "^2 = " . (n*n) . "<br>"; } ?>