Can anyone please explain me this code, why $name came instead of $names | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone please explain me this code, why $name came instead of $names

<?php $names = array("John", "David", "Amy"); foreach ($names as $name) { echo $name.'<br />'; } ?>

11th Feb 2017, 9:33 AM
Harsha Deep
Harsha Deep - avatar
1 Answer
+ 4
foreach executes as many times as there are values in the given array ($names). Each time it's run it stores the current value in the given variable ($name). $name can then be used inside the foreach-block. Also, $name is probably read-only. (No gurantee.)
11th Feb 2017, 11:24 AM
Maike