Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

how do I loop $names in for each loop challenge task 2 of 2? can't see to get it

how do I loop $names in for each loop challenge task 2 of 2? can't see to get it

index.php
<?php 
$names = array('Mike', 'Chris', 'Jane', 'Bob');
?>



<?php
foreach($names as $name){
?>

  <?php echo $name?>

<?php
}
?>

2 Answers

Gianmarco Mazzoran
Gianmarco Mazzoran
22,052 Points

Hi,

you can write your foreach loop inside of one php tag.

And in order to pass the challenge, seems like you forgot to put a semi-colon! Can you guess where?

You are missing a semi colon on this line:

 <?php echo $name?>
//Shoud be 

 <?php echo $name;?>

You can also omit most of your php tags since you aren't mixing any other content like HTML in your code yet

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

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

?>

Keeping your code cleaner like the code above can help aid in catching errors.