Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.php
<?php

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

<?php
//   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