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 trialJonas Støvring
5,229 PointsHow to print the arrays content with a foreach loop
I can't seem to figure out how to make a foreach loop print the content of my array. What ever I have tried, it tells me that I task 1 no longer is passed ;) Hope somebody can help me
<?php
$letters = array("A","C","M","E");
echo "Today's challenge is brought to you by the ";
echo count($letters);
echo " letters: ";
echo "AC";
echo ".";
?>
5 Answers
Steven Parker
231,261 PointsIt doesn't look like you started on task 4 yet. You should get a message that says "Bummer! You need to use a foreach loop within your code."
You still need to replace the "echo "AC";
" line with a loop that prints out the array.
Jonas Støvring
5,229 PointsHi Steven.
Thank you for your quick response. The code included in my question is without my several tries...
Should i replace echo "AC"; with a print function ?
Steven Parker
231,261 PointsThe instructions say, "Replace the fourth echo statement with a foreach loop that prints each letter in the array."
The loop might have an "echo" statement inside it.
Jonas Støvring
5,229 Points<?php
$letters = array("A","C","M","E"); echo count($letters); foreach($letters){ echo "$letters"; } ?>
I tired with this, cant seem to see what I am missing
Steven Parker
231,261 PointsYou don't have the syntax of the "foreach" statement quite right:
foreach (array_expression as $value)
For more information, see the documentation page.
Jonas Støvring
5,229 PointsI will try that. Thank you for your help Steven :)
Jonas Støvring
5,229 PointsIt worked perfectly :) Amazing job Steven