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 Integrating with PayPal Array Keys

Code Challenge echo array $key so that the title and the isbn display on same line in brackets.

Really becoming clear that I am not understanding this though I am writing in all examples and typing in all project code. Is there something else that is good to read to reinforce the concepts?

This solution is based directly off project code but it doesn't work and though I have created other non working solutions this seems to be the method shown prior.

<body>
    <h1>Five Great Books</h1>
    <ul>
        <?php foreach($books as $isbn => $book) { 
                echo "<li>";
              echo $book;
              (echo $isbn);
              echo "</li>";
        } ?>
    </ul>
</body>

2 Answers

Stone Preston
Stone Preston
42,016 Points

the issue here is the way you are echoing everything out. It looks like you understand what needs to happen, however the way you are trying to achieve it is a little off.

what you have now:

echo "<li>";
              echo $book;
              (echo $isbn);
              echo "</li>";

is going to look something like this after its echoed out

<li>Some book Name10015</li>

as you can see there need to be some spaces and there are no parenthesis (putting parenthesis around the echo statement doesnt do anything)

you are going to need to concatenate some spaces using the . operator. Like I said its easier to see what you need to do if you just use one echo statement and concatenate everything together. here is an example of using concatenation with the . operator: (assume someVariable = 2 and another variable = 3)

 echo "<li>" . $somevariable . " " . $anothervariable . " being printed out" . "</li>";

will output

<li>2 3 being printed out</li>

notice how I concatenated a space using . " " and also included a space on the beginning of some of the words

Thanks that was it indeed, I guess I was over thinking it a bit and it was the basic stuff tripping me up.

I get the right result by using the following code, however I can't pass the challenge. I get the following message "Bummer! I see all the book titles and the ISBNs in the browser, but the formatting is not quite right. Each list item should look like this: Book Title (978-0123456789)"

<head>
    <title>Five Great Books</title>
</head>
<body>
    <h1>Five Great Books</h1>
    <ul>
        <?php foreach($books as $isbn => $book) { ?>
            <li>
              <?php echo $book;?>
              (<?php echo $isbn; ?>)</li>
        <?php } ?>
    </ul>
</body>
</html>

I have now changed it to

              <?php echo $book . " (" . $isbn . ")"; ?>

It displays correctly but I cannot pass the challenge.

I have added the <li> items inside the <?php ?> and now it works. This is a tricky question for a code challenge, since you can get to the outcome in many different ways.