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?
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.
+ 2
See PHP manual description:
http://php.net/manual/en/control-structures.foreach.php
+ 1
it's like take one by one element in to value variable from Colors till the end and print them
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 ~