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

Stuck on Code Challenge in Build a Simple PHP Application

So it's time for me to call on some help. I am stuck on a code challenge. So I get the first part correct which brings my code to:

<?php
$letters = array("D","G","L");
echo "My favorite ";
echo count ($letters);
echo " letters are these: ";
echo "AB";
echo ".";
?>

The next step gets me confused, it says: 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.)

So my code is:

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

I find what is being asked very confusing and then it starts to say that my code is now not recognised for the task before. Any help much appreciated as I have tried and tried but I'm proper stuck.

How can I make it so that the php code outputs 'my favorite letters are ABABAB.' at the end of this without changing anything else?

4 Answers

So it looks like you got the lines wrong, and you got ahead of yourself. Basically what it's asking is that the only line inside the foreach loop is the one that says echo "AB";. So your loop needs to start below echo " letters are these: "; and should contain that line. You're not echoing out $letter quite yet.

Hey Stephen,thanks for your help. I finally got it!!! This had been driving me mental. You're right I was jumping ahead but I don't think the instructions are very clear. Thanks again for your help :)

Developers are hot these days... :)

Melanie as Stephen Crockett mentioned you are getting ahead. The goal of the quizz is to go step-by-step so that you can understand what the code is doing regardless of what the goal is. In this case I assume they want to be sure that you understand that the foreach loop is looping the code inside 3 times because it's the array length.

<?php

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

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

?> 

Ivo Miranda

Please refrain from making sexual harassing or otherwise creepy remarks in reference to other Treehouse members.

Such remarks are in violation of section 2.3.1 of this site's Terms and Conditions

James Barnett Wow. Sorry I didn't meant to be rude. Thanks for the warning :)

From what i know treehouse has 0 tolerance so i guess there aren't any warnings .

Konrad Pilch why you say that? I was warned and everything was fine. It was just a latin comment but if people find it that offensive I can just delete it. I would prefer leave it like that since we are all humans and it's good if someone learns from the mistakes of others in programming or in virtual interaction with other humans :P

Well, you can trust me that one of my friend on treehouse was a mod. He then got suspended straight away with 0 Tolerance , like they say in they codex with no warnings and didn't came back.

Im surprised you got a warning since teamtreehouse clearly say they have 0 tolerance . I know that people make mistakes etc.. but why not give everyone equal chance since everyone are all humans ^^ And this happened like 3 months ago . There were more people involved too . Well i saw the screenshots .

Wow that's strange... Well if I get banned for one of those reasons I wouldn't want to comeback anyways. Don't want to belong in a group of Nazis or whatever that have zero tolerance towards a little mistake. Plus there is codeschool and other schools that pretty awesome as well.

Well , the thing about treehouse is that they have like top quality content and you can learn pretty fast and you have forum too.

Im sure or probably no STAFF saw this . A MOD isnt a STAFF is a normal student thats just helping out etc..

Nazis , somebody told me that too . I remember when that guy showed the conversation to an english teacher and he said "Nazis" . I just know that it made harder for the guy .

I still don't get this one... I'm doing everything correct and can't figure out how what they are looking for in step 6?????

<?php

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

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

?>

What is wrong with this?

This is the error I'm getting "You seem to have too many letters. You should only be displaying one letter inside your foreach loop." Why should I only be displaying on letter inside the loop? That isn't what they are asking for!

I got the same error with your code, but it works if you remove the newline (\n) that you're adding right before the foreach loop.

Thanks... That did it. Not sure why that would hang it up, but such is life. Cheers!

that helped tremendously, thank you so much Stephen and Russell. :)