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 Basic PHP Website (2018) Listing and Sorting Inventory Items Creating the Catalog Array

thimoschroeder
thimoschroeder
23,470 Points

Creating the Catalog array - echo & carnation question

Hey, we created an array in the catalog.php folder:

<?php $catalog = array( "Design Patterns", "Forrest Gump", "Beethoven" );

and then moved on to to echo the code with the following:

<?php foreach($catalog as $item) { echo "<li>" . $item . "</li">"; } ?>

I didn't understand how to include the li's inside the code and what the "." should display !

Hope, that someone is able to clarify this.

3 Answers

You've done everthig correct except one small thing, additional double quotes inside of the double quotes. If you are putting same quotes inside of the same quotes than you should escape them like this:

echo "some text \" inside of some text\"";

dot sign inside of echo statement means string concatenation, it's a part of php language(and you wont see them in output). In code below you are adding text(which you receive from array) into <li></li>(list items). So, when you render the page you'll receive this output:

<li>Design Patterns</li>
<li>Forrest Gump</li>
<li>Beethoven</li>

Working code:

$catalog = array( "Design Patterns", "Forrest Gump", "Beethoven" );

foreach($catalog as $item) {
    echo "<li>" . $item . "</li>";
}

Hope this explains how thing works ;)

Chris Davis
Chris Davis
16,280 Points

you could also use...

echo "<li>$item</li>";
thimoschroeder
thimoschroeder
23,470 Points

Serhii Lihus - all right, thanks for your help, this clarifies the issue I ran into !