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 trial

PHP PHP Basics (Retired) PHP Conditionals & Loops PHP Loops Challenge

''' <?php $names = array('Mike', 'Chris', 'Jane', 'Bob'); foreach ($names as $names) { echo $names; } ?> '''

echo $names;

why is this not working?

index.php
<?php 
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $names) {
    echo $names; 
}
?>

i also tried same code as your and it is working for me

<?php 
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach($names as $names){
  echo $names;
}
?>

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Because $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! :thumbsup:

thank you very much for quick answer