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

Shilpa Kothari
Shilpa Kothari
4,518 Points

Please help

Can you please tell what is wrong in my code.

books.php
<?php

$books = array (
978-0743261690 => "Gilgamesh";
978-0060931957 => "The Odyssey";
978-0192840509 => "Aesop's Fables";
978-0520227040 => "Mahabharta";
978-0393320978 => "Beowulf"
 ); 
?>
<html>
<head>
    <title>Five Great Books</title>
</head>
<body>
    <h1>Five Great Books</h1>
    <ul>
        <?php foreach($books as $isbn => $book) { ?>
            <li><?php echo $book . ' (' . $isbn . ')'; ?></li>
        <?php } ?>

    </ul>
</body>
</html>

2 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

When you create the items in the array, you are supposed to use commas to separate each line, not semi-colons.

<?php
$books = array (
978-0743261690 => "Gilgamesh",
978-0060931957 => "The Odyssey",
978-0192840509 => "Aesop's Fables",
978-0520227040 => "Mahabharta",
978-0393320978 => "Beowulf"
 ); 
?> 
Shilpa Kothari
Shilpa Kothari
4,518 Points

I Changed the semicolon into commas but now it is saying ISBN is not seen in the browser.

Hi,

leave the array as it was like this:

<?php

$books["978-0743261690"] = "Gilgamesh";
$books["978-0060931957"] = "The Odyssey";
$books["978-0192840509"] = "Aesop's Fables";
$books["978-0520227040"] = "Mahabharta";
$books["978-0393320978"] = "Beowulf";

?>
Jeff Lemay
Jeff Lemay
14,268 Points

I copy/pasted your code and got this error at first: Parse error: syntax error, unexpected ';', expecting ')' in C:\xampp\htdocs\today.php on line 4

Then I updated the array with commas instead of semi-colons and got this:

Five Great Books Gilgamesh (-1978812) The Odyssey (930) Aesop's Fables (977) Mahabharta (-88156750) Beowulf (975)

I assumed that was correct since it printed your foreach loop but just now I noticed the ISBNs aren't printing properly so I wrapped each key in quotes like it is a string and then got:

<?php
$books = array (
'978-0743261690' => "Gilgamesh",
'978-0060931957' => "The Odyssey",
'978-0192840509' => "Aesop's Fables",
'978-0520227040' => "Mahabharta",
'978-0393320978' => "Beowulf"
 ); 
?>
<html>
<head>
    <title>Five Great Books</title>
</head>
<body>
    <h1>Five Great Books</h1>
    <ul>
        <?php foreach($books as $isbn => $book) { ?>
            <li><?php echo $book . ' (' . $isbn . ')'; ?></li>
        <?php } ?>

    </ul>
</body>
</html>

Five Great Books

Gilgamesh (978-0743261690) The Odyssey (978-0060931957) Aesop's Fables (978-0192840509) Mahabharta (978-0520227040) Beowulf (978-0393320978)

Shilpa Kothari
Shilpa Kothari
4,518 Points

Thank you all everything is working.