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

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

Help

Why isn't this code working?

letters.php
<?php


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

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

?>

2 Answers

Henrik Hansen
Henrik Hansen
23,176 Points

You could automize the number of letters this way:

<?php


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

$stringToPrint = "My favorite " + count($letters) + " are these: \n";

foreach($letters as $letter){
    $stringToPrint += $letter + "\n";
}
echo $stringToPrint; 

?>