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

Introducing Arrays task 5 of 7 Stuck - I've read the other post about this but still can't get it

<?php
$letters = array("D", "G", "L");
echo "My favorite ";
echo count($letters);
echo " letters are these: ";
    foreach ($letters as $letters){
    echo $letters;

echo "AB";}
echo ".";
?>

The output I get is My favorite 3 letters are these: DGLAB.

[ed. note:] Added markdown, to aid code formatting

6 Answers

Hi Angela,

There are two issues with the code you have here. The first is that in your foreach statement, you overwrite the value of your $letters array by using $letters as your working variable. Your working variable should be $letter (singular).

The second issue is that you have an extra echo statement below your foreach statement. I don't know if you just included that there for testing, but that should be removed.

Here is the adjusted code:

<?php

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

?>
Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Hey @Angela,

The sentence says "These are my 3 favorite letters," but the output you have shows five letters. At this step in the code challenge, you should show the three letters. Your output should be this:

My favorite 3 letters are these: DGL.

Does that help?

When I replied earlier I went back and looked at the instructions for Step 5. The last sentence says this:

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

It's not until Step 6 that the AB is replaced with your actual favorite letters.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(Oh, you're right Carter. Sorry about that. Got ahead of myself there. :-)

Angela, It's actually alright that the output is DGLAB at this point. (I meant for you just to write the foreach loop and have ABABAB, but you have anticipated part of the next step.) Carter is right about the main issue. The instructions ask you to use a working variable named $letter (singular) in the foreach loop.

Does all that help? (Sorry for the confusion with my earlier reply.)

but in the first step is says to create an array that says letters not a singular.

The code block below displays a list of two letters from the Latin alphabet using echo statements. But these two letters are NOT my favorite letters! In this code challenge, we’ll change this block of code to display my real favorite letters. We’ll also modify it to use an array so that it’s easier to add a new letter in the future. Before the first echo statement, create an array called letters. Give that letters array one element with a value of “D”.

AH! I finally got it. I was just misunderstanding the directions

foreach ($letters as $letter)

This is my completed code that works. Thanks!!

<?php

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

?>