0

What does $value and $key mean in a foreach loop (PHP)

A basic foreach loop looks like this: <?php $colors = array("red", "green", "blue"); foreach ($colors as $value) { echo "$value <br>"; } ?> My question is why does it say $value after $colors and what does it actually do to the code? Why must you have it for a foreach loop to work?

9th Apr 2018, 1:26 PM
PathFinder
PathFinder - avatar
4 Réponses
+ 2
foreach loop iterates over each element in $Colors and collects each value in a local variable called $Value so you don't have to deal with index or length of array.
9th Apr 2018, 1:34 PM
Pavan Kumar T S
Pavan Kumar T S - avatar
9th Apr 2018, 2:30 PM
Ipang
+ 1
it's like take one by one element in to value variable from Colors till the end and print them
9th Apr 2018, 7:27 PM
Pavan Kumar T S
Pavan Kumar T S - avatar
0
@Pavan Kumar T S So what you are saying is once the loop has been executed you can access the data locally from the value variable aswell as the colors variable? Sorry in advance if I'm asking too many questions ~
9th Apr 2018, 4:12 PM
PathFinder
PathFinder - avatar