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

I'm stuck on this question in the code challenge

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. (Leave the letters AB in the fourth echo command for now. At the end of this step, the page should say my favorite letters are ABABAB.)

Can someone please help me with this code


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

echo "My favorite ";
echo count ($letters);
echo " letters are these: ";
echo "AB";
echo ".";


2 Answers

Hey Phillip,

NOTE ADDED: The treehouse platform has been under maintenance this morning. That could have something to do with it, but let's go over foreach loops just for good measure.

Glad your are feeling better. No need to repost the question, lets just go through the answer step by step.

//   The array of letters you built contains three strings. 
$letters = array("D","G","L");

echo "My favorite "; // Start the sentence
echo count($letters); // Count and echo out how many items are in the $letters array 
echo " letters are these: "; 
foreach($letters as $letter) { // On every iteration hold an individual item in 
 // the $letters array in a temporary variable called $letter
echo "AB";  // Every time the foreach loop runs, echo "AB"
}   // Close the foreach loop
echo ".";                                

I recommend you rewrite it even though that code would work how it is. Foreach loops are extremely important in php. Stick with it and it gets much easier.

Good luck!

Thank you sir, I'll try the challenge again later today and don't worry I will not give up especially when I know there are people like you willing to help people like me learn this stuff. Thanks again for your help and you have a great day.

Foreach loops cycle through an array. Each cycle is called an iteration. Since there are three items in the $letters array, a foreach loop will iterate three times. Each iteration should echo the string "AB". So the answer is:

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

Hope this helps!

Stephen, first I want to thank you for your help and apologize for taking so long to respond to your post I have not been feeling too well. Anyway there seems to be another issue with this question as it is still counting it as being wrong, I'm thinking that I'm not fully understanding where they are saying to insert the foreach loop and brackets if possible could you please help me with this again? If you need me to re-post the question I can just let me know