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

Where do i put echo?

I can't figure out how to echo each of the names to the screen Inside of the foreach loop :(

index.php
<?php foreach ($names as $name){ 

     $names = array('Mike', 'Chris', 'Jane', 'Bob');?>

}  
?> </li>

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

You'll want to move your array outside of the foreach loop, so instead your for each loop is going to grab the first value of array $names and assign it a temporary variable called $name (this is a common naming convention to name arrays as plural and use their singular form as the temp variable). You also have a mismatching PHP closing bracket and the end of your array.

Than, in the for each loop, just echo the $name variable. So it would look like this

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

    echo $name

}  
?>