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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Introducing Arrays

How 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

index.php
<?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
Steven Parker
231,248 Points

It 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.

Hi 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
Steven Parker
231,248 Points

The 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.

<?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
Steven Parker
231,248 Points

You don't have the syntax of the "foreach" statement quite right:

foreach (array_expression as $value)

For more information, see the documentation page.

I will try that. Thank you for your help Steven :)

It worked perfectly :) Amazing job Steven