Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: