I am not able to understand the example of foreach loop and what is the foreach ($name is $name)in it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I am not able to understand the example of foreach loop and what is the foreach ($name is $name)in it

20th Jun 2016, 5:51 PM
Priyanka
2 Answers
0
foreach is used to iterate through an array. consider an example : $fruits = array('apple','orange','banana'); now we want to get each element of the array fruits. So we can use for each. foreach($fruits as $fruitname) { echo $fruitname; } here $fruitname is any arbitrary name for the variable. Each time the loop iterate, value of the current element is assigned to $fruitname. so first iteration $fruit name will be apple and will echo apple and pointer is incremented. next $fruitname will be orange. and next banana.
21st Jun 2016, 4:36 AM
sitharamubeen
0
foreach loop is used for array. if we want to get each element of an array then we can foreach loop in php..for example $color= array('red','green','black'); foreach($color as $value) { echo $value; } all colors will display on screen. $value is a copy of variable color. you can use any other name its up to u..foreach loop is used for array..when loop execute first time then then value on 0 index is red. pointer then move on second index then the value will get that is green and so on
21st Jun 2016, 3:27 PM
Tayyaba Butt
Tayyaba Butt - avatar