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 trialShilpa Kothari
4,518 PointsHow 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
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $name)
{
echo "$name <br>";
}
?>
1 Answer
Ruben Carrizales
8,445 Pointstry this:
echo $name."<br>";
Ruben Carrizales
8,445 Pointsinside of quotes marks add the break tag
ole haugset
Courses Plus Student 3,892 Pointsole haugset
Courses Plus Student 3,892 PointsThat 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.