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

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

Hi! I put the following thing: foreach( $names as $names){ echo $names};

And it won't accept it. its probably just a small thing thats wrong. Can someone please help me out here? Thanks in advance

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

2 Answers

Two things I would like to point out. Where you have "echo $names};" should be "echo $names; }". Note the placement of the curly brace "}" is after the semi-colon.

Secondly, where you have "foreach( $names as $names)" you're using the variable $names twice. Try "foreach( $names as $name)". Note how I used name in singular. More importantly it has to be something other than $names since it can't be the same.

Lastly, echo out $name and it should work.

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

Well said in your answer, Juan. In the future it may be helpful to also share the improved code along with your explanation so that other, including the original poster, can learn from it as well. It's definitely helpful and encouraged around here to accompany a well explained answer. Thanks for helping out! I've edited your answer to include the code you explained.

You are the best

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

thanks Juan. That helped. I already thought it would be the semicolon again. keeps happening to me

Hi Daniel,

If Juan's recommendation helped answer your question satisfactorily and you don't have any further questions, we recommend you select Juan's answer as the "Best Answer" which you can click on just below his answer. This rewards Juan for providing a thorough and well explained answer and also helps other members know that this questions received an approved answer. Thank you!