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 Displaying Categories

Pawandeep ShopWired
PLUS
Pawandeep ShopWired
Courses Plus Student 9,848 Points

Parse error: syntax error, unexpected end of file in /home/treehouse/workspace/index.php on line 27

cant view the website, don't understand what I am doing wrong the code is:

<?php 
include("inc/data.php");
include("inc/functions.php");
$pageTitle = "Personal Media Library";

include("inc/header.php"); ?>
        <div class="section catalog random">

            <div class="wrapper">

                <h2>May we suggest something?</h2>

        <ul class="items">
            <?php 
     $random = array_rand($catalog,4);
var_dump($random);
         foreach($random as $id) {
           echo get_item_html($id,$catalog[$id]);
       ?>                           
                </ul>

            </div>

        </div>
<?php 
include("inc/footer.php"); 
?>
Pawandeep ShopWired
Pawandeep ShopWired
Courses Plus Student 9,848 Points

sorry, & I realised I hadn't completed the operator on Line 10 but now I have done that and here is the code, the message is still there it doesn't display nothing, can you spot anything?

<?php
$catalog = [];
$catalog[101] = [
    "title" => "Design Patterns",
    "img" => "img/media/design_patterns.jpg",
    "genre" => "Tech",
    "format" => "Paperback",
    "year" => 1994,
    "category" => "Books"
    "isbn" => "978*0201633610",
    "publisher" => "Prentice Hall",
    "authors" => [
      "Erich Gamma",
      "Richard Helm",
      "Ralph Johnson",
      "John Vlissides"
    ]
Greg Kaleka
Greg Kaleka
39,021 Points

You're missing a comma after "Books" and before "isbn".

Another tip: Always check the line before the error as well as the line the error occurred on. Often that's where the actual problem lies. PHP said "hey what's this 'isbn' thing? I was expecting a coma or a closing square bracket".

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Pawandeep,

Unexpected end of file usually means you forgot to close something, and indeed - you never closed your foreach loop. Make sure you get a } in there :blush:. It's tricky because the line number given is just where the file ends. Just remember that this error means you opened and didn't close something somewhere.

Cheers :beers:

-Greg

Pawandeep ShopWired
PLUS
Pawandeep ShopWired
Courses Plus Student 9,848 Points

I have now ensured I've closed the index.php and the same message is still coming through

but Thank you so much for your help, I shall bare that in mind next time

Greg Kaleka
Greg Kaleka
39,021 Points

Can you share your updated code? It's this foreach loop that never gets closed:

foreach($random as $id) {
           echo get_item_html($id,$catalog[$id]);
Pawandeep ShopWired
PLUS
Pawandeep ShopWired
Courses Plus Student 9,848 Points

now a new message is saying **

Parse error: syntax error, unexpected '"isbn"' (T_CONSTANT_ENCAPSED_STRING), expecting ']' in /home/treehouse/workspace/inc/data.php on line 10

my updated code:

<?php 
include("inc/data.php");
include("inc/functions.php");
$pageTitle = "Personal Media Library";

include("inc/header.php"); ?>
        <div class="section catalog random">

            <div class="wrapper">

                <h2>May we suggest something?</h2>

        <ul class="items">
            <?php 
     $random = array_rand($catalog,4);
var_dump($random);
         foreach($random as $id) {
           echo get_item_html($id,$catalog[$id]);
         }
       ?>       
                </ul>

            </div>

        </div>
<?php 
include("inc/footer.php"); 
?>
}
Greg Kaleka
Greg Kaleka
39,021 Points

Pawandeep, your error isn't in this file - it's in data.php. See that in the error message?

Also, please try to add comments as comments, not as answers :blush: