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

ANS to this line. Inside of your foreach loop echo each of the names to the screen.

How can I write this?

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

1 Answer

Erik McClintock
Erik McClintock
45,783 Points

Vijay,

You're so close! You have your temporary $name variable inside a string, which is what's causing your issue here.

You have:

foreach( $names as $name ) {
    echo "$name <br>"; // note how $name is inside quotation marks here, i.e. inside a string datatype
}

You want:

foreach( $names as $name ) {
    echo $name; // note how we have removed the quotation marks, and thus we will retrieve the value stored in the given variable
}

If you want to keep the line break after each name, you can use simple concatenation in PHP to achieve this:

foreach( $names as $name ) {
    echo $name . '<br>';
}

Happy coding!

Erik

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Actually, variables inside double quotes in PHP will be interpreted correctly, although it's not so great for code readability.

The problem here is the <br> tag that you have, Vijay. It'd work fine in real code, but it's causing the challenge to fail so just remove that (and the quotes, just for good measure), and you should be good to go! :)