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
207,973 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
207,973 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
207,973 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