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 Listing and Sorting Inventory Items Associative Arrays

"$catalog = [];" is missing in the data file containing all $catalog arrays

In "Building the basic PHP website" the teacher shows how to add items to the array starting with creating an array variable using shortcut notation like this: "$catalog = [];, and then she adds the catalog arrays to it. However in the array file attached in the teacher's notes the initial "$catalog = []; is missing... How come the code still works?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Marcin Lipski! If we did $catalog = []; that would set us up with a blank array. Then we could add to it gradually over time. But that's not absolutely necessary we also have the option to just define the array at the time we create it so that it isn't empty.

The very first entry there is this:

$catalog[101] = [
    "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software",
    "img" => "img/media/design_patterns.jpg",
    "genre" => "Tech",
    "format" => "Paperback",
    "year" => 1994,
    "category" => "Books",
    "authors" => [
        "Erich Gamma",
        "Richard Helm",
        "Ralph Johnson",
        "John Vlissides"
    ],
    "publisher" => "Prentice Hall",
    "isbn" => '978-0201633610'
];

At that point, the array is defined. An array does not have to be empty first. There are some occasions where you will first want an empty array. But it's not always necessary. Also, remember that she is just reading in from this txt file. At this point, it's not yet executable PHP. It doesn't contain the <?php tags nor the .php extension.

Hope this helps! :sparkles: