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

Shilpa Kothari
Shilpa Kothari
4,518 Points

How to echo each name to the screen.

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

It says to echo each name to screen. I have no idea how to do it.

Thanks

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

?>
ole haugset
ole haugset
Courses Plus Student 3,892 Points
foreach( $names AS $name ){
 echo $name; 
}

That should do the trick for you. The " is only necessary when you are echoing a string. In this case you are echoing a variable. Also, the BR-tag is not necessary to complete this task as it is not defined in the challenge.

However, if you are going to use variables inside a string when echoing, the correct syntax would be:

echo "{$name} <br />";

The {}Β lets PHP explicitly know to treat the content as a variable or object, and not as text in a string. You can do it the way you did aswell, but it will not work later on when you start to work with objects.

Hope this helps.

1 Answer

Ruben Carrizales
Ruben Carrizales
8,445 Points

inside of quotes marks add the break tag