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!

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

How do we do it?

I am stuck at this, the array says "D","G","L". whereas foreach result should be ABABAB. i am confused

3 Answers

Hi Ammar,

In your screenshot you're missing the closing curly brace } for the foreach loop between lines 9 and 10. It should come right after echo "AB";


$letters = array("D", "G", "L");

echo "My favorite ";
echo count($letters);
echo " letters are these: ";
foreach($letters as $letter) {
    echo "AB";
echo ".";


We've set up the loop so that the working variable $letter will be assigned each of the letters in our $letters array. We know that the loop will run 3 times and $letter will get each of the values: "D", "G", and "L" as it loops 3 times. Just because we have that $letter variable, it does not mean that we have to use it. If you look inside the body of the loop, there is only a single statement - echo "AB";

We do not use the $letter variable anywhere inside the loop which is why we don't see any letters from our array. All that happens each time is that we echo "AB" to the page. Since it loops 3 times, we will see "ABABAB".

I think it will make more sense once you complete the remaining tasks.

I think you're at task 5? Is that an error message you're getting or you're just confused why it's like that?

the foreach loop at that point looks like this:

foreach ($letters as $letter) {
echo "AB";

Since $letters has 3 items in it, that loop is going to execute 3 times. At this point in time it will echo "AB" 3 times.

Maybe you can post your code. I'm not sure if you're getting errors or just don't understand why it's doing what it's doing.

http://awesomescreenshot.com/0e52je5rd0 here is screenshot. how can we have ABABAB in the end when array says different words?