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 Sorting Array Items

How can you put the two if statements together? If you use OR, doesn't it always display all items? So confused...

The first if statement makes sure that if there's no category we get all the keys, the second one makes sure if there IS one, only those are shown. It doesn't make sense to me that they can be put together, why does is not always show all items like this? I mean, every item is true for one of the two?

3 Answers

At the beginning of each loop irritation, PHP checks if the first condition is true. If it is - PHP trims and sorts the current item (not every item) and never proceeds to the second condition. When looping through the catalog arrays is finished, all the items are shown on the page. However - if the first condition is false (meaning $section is not equal to null), PHP moves to the second condition and checks if it's true. If it is - only the items that match the second conditional statement are being trimmed, sorted and displayed on the page.

The condition $category == null is used to display all items

The condition strtolower($category) == strtolower($item["category"]) is used to display items for a particular category. For example if $category is "Books" then books display but not music or movies so not all items are included. Every item is not true for one of the two. In the example I gave for a catalog item with category "Music", $category of "Books" isn't null but also "books" doesn't equal "music" so both conditions are false.

Josh Coast
Josh Coast
5,112 Points

I have the same question, and I still don't understand. The if statement is doing the same thing for both conditions. Wasn't the whole point to do different things?