Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMilos Ibrocic
Courses Plus Student 841 Points''' <?php $names = array('Mike', 'Chris', 'Jane', 'Bob'); foreach ($names as $names) { echo $names; } ?> '''
echo $names;
why is this not working?
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $names) {
echo $names;
}
?>
1 Answer
Jennifer Nordell
Treehouse TeacherBecause $names is the name of the array, you can't use $names again for the items in the array. They need their own variable name. And that's what we're going to echo back to the screen. Take a look:
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $name) {
echo $name;
}
?>
edited for clarification
What we're saying here is that our list of names is named $names. Every item in there is a $name. Mike is a name. Chris is a name etc. But $names is the list holding our names. Hope that makes sense!
Milos Ibrocic
Courses Plus Student 841 Pointsthank you very much for quick answer
Ashish Mehra
Courses Plus Student 31 PointsAshish Mehra
Courses Plus Student 31 Pointsi also tried same code as your and it is working for me