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

HELP: Introducing Arrays

First and foremost hello to all students of treehouse, im Lans.

I have been working pretty well through the 'Build a Simple PHP Application' course so far but have now stumbled upon this problem;

I don't know if it's because I don't understand the question or what but i've watched the video a few times now and still can't get my head around it. If someone could take a look and give me some advice that would be great.

The question im stuck on in the challenge is Task 6:

"The fourth echo statement, the one inside the foreach loop, should only display one letter at a time. Change that fourth echo statement so that it does not display the static piece of text AB; instead, make it display the value for the array element under consideration inside the foreach loop."

and here is my code so far:

<?php

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

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

}
echo "AB"
echo ".";

?>

Like I said, I have tried a lot with it and I am just looking to learn were I have gone wrong.

Thanks for reading :D

7 Answers

Here's your code formatted a little differently:

<?php
$letters = array("D", "G", "L");
echo "My favorite "; 
echo count($letters); 
echo " letters are these: \n"; 
foreach($letters as $letter) { echo $letter. "\n"; } 
echo "AB"
echo ".";
?>
  • In PHP every statement must end in a semicolon.
  • Do you see a statement that is missing a semicolon?

Now that your code runs, the directions state:

  • Change that fourth echo statement so that it does not display the static piece of text AB
  • So, remove the code that will cause the piece of text AB to no longer display.

  • Finally, the directions don't call for newlines
  • So you can remove the code that is creating the newlines

When you are done the output should exactly match this:

My favorite 3 letters are these: DGL.

Great!

Thanks for the help James, Ive no managed to complete the task . After reading your comment I went back and looked at the code and realised my simple mistakes haha. Maybe i spent too much time staring at it earlier :P

Once again thanks for the help!

@Lans - I had a professor in college that said ...

Half of all troubleshooting can be solved by going to lunch.

Sometimes you just need to look at it with fresh eyes, then the issue suddenly becomes obvious.

"Half of all troubleshooting can be solved by going to lunch." Brilliant! Should be a T-shirt.

@James - Your professor spoke wise words!

Good hustle @James - you're the man :)

This helped