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

Logan R
Logan R
22,989 Points

Am I missing something with my foreach?

Question: Inside of your foreach loop echo each of the names to the screen.

My code:

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

It's saying: I do not see any output yet, remember to use echo

Am I forgetting to do something? Thanks!

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

Yeah you flipped the variables in the foreach condition. It should be:

foreach( $names as $name){
  echo $name;
}

not:

foreach( $name as $names){
  echo $name;
}

Right now you are ask php to iterate over a variable, $name, that doesn't exist. Hope this helps.

Logan R
Logan R
22,989 Points

Ah, thank you xD