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 Simple PHP Application Listing Inventory Items Introducing Arrays

1) Add the start of a foreach loop with an opening curly brace between the third and fourth echo commands. The foreach loop should load each element from the letters array, one at a time, into a working variable named letter. (2) Add the closing curly bra

$letters= array("D","G","L"); echo "My favorite "; echo "3"; echo " letters are these: "; echo "AB"; echo ".";

Logan R
Logan R
22,989 Points

Can you please clarify the question?

Right now, the text shows the wrong letters; we want it to display my real favorite letters from the array. In the next few tasks, we’ll change that. This task has two steps. (1) Add the start of a foreach loop with an opening curly brace between the third and fourth echo commands. The foreach loop should load each element from the letters array, one at a time, into a working variable named letter. (2) Add the closing curly brace for the foreach loop between the fourth and fifth echo commands

Logan R
Logan R
22,989 Points

lol, yes I got that, but what is your question about it? Are you asking what the answer is? Are you confused on some part of it?

i need the answer i don't understand what it is asking

4 Answers

Logan R
Logan R
22,989 Points

Ah OK, so what they want you to use is a foreach loop.

foreach($arrayString as $newArrayString) {
    echo $newArrayString;
}

$arrayString is the value of the string array. In this case, it would be $letters.

$newArrayString is what the $arrayString is renamed too, in this case $letter.

The foreach loop will echo each array value one at a time and will start with the code right after the {, then stop at the } and go back to the { until all arrays have been echoed.

If you are not completely understanding this, they go over how to use a foreach loop in great detail a video or two before the coding quiz :)

The fourth echo statement, the one inside the foreach loop, should display my favorite letters from the array INSTEAD of AB. Remove the static piece of text from that fourth echo statement, changing it instead to display the value for the array element under consideration inside the foreach loop.

$letters = array("D","G","L"); echo "My favorite "; echo "3"; echo " letters are these: "; foreach($letters as $letter){ echo $letter[D]; echo $letter[G]; echo $letter[L]; echo "AB"; } echo ".";

Logan R
Logan R
22,989 Points

Closer.

foreach($letters as $letter){
   echo $letter;
}

It will auto cycle through all the letters.

it didnt work.

Louis Philippe Savard
Louis Philippe Savard
5,378 Points

You have to close your curly brace after echo"AB"