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

Kelly Brown
Kelly Brown
2,418 Points

What's wrong with my code?

Hello World!

Not really sure where I have gone wrong. I have tried multiple variations, but am utterly stuck. Could someone please share with me the final code so I can see where I went wrong?

Thanks so much!

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

<?php
foreach($names as $name){
  <li> echo '$name' </li>
}
?>

3 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

You are very close, for starters you can write your code inside of the given php block. For this challenge you don't need the list item tags so you can eliminate those, you also need to echo out the content of the variable $name not a string so there is no need to surround $name with quotes. Also don't forget your semi-colon at the end of your echo statement.

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

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

?>
Chris Adamson
Chris Adamson
132,143 Points

To accomplish what you are trying to do, you need to move the li element into Strings, and use the concatenation operator ("."):

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

<?php
foreach($names as $name){
  echo "<li>" . $name . "</li>";
}
?>

You don't have to use concatenation here, although you can. You can encapsulate the $name variable within double quotes along with the <li> elements. You just can't use single quotes to do so; you have to use double quotes.

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

foreach ($names as $name) {
  echo "<li>$name</li>";
}
?>
Kelly Brown
Kelly Brown
2,418 Points

AHH! That makes so much sense! Thank you both so much for the quick responses!