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 Building a Media Library in PHP Working with _GET Variables

Ker Sing Tan
Ker Sing Tan
10,572 Points

Why isset ?

I am not quite understand why isset() will make the undefine error gone? Since we have set the if statement for all 3 catalogs $_GET["cat"], why have to use isset() ?

1 Answer

Hello Ker Sing Tan , In this video, the "if" statement is not actually setting the variable of cat. The if statement is only checking to see if the "cat" variable is set to a specific string such as "books" or "music" or "movies". When the instructor adds ?cat="music" to the URL, this is what is setting the variable of "cat". So when she removes this from the URL in the video, "cat" is undefined because the variable is not being set to anything. When she adds the isset() function to $_GET["cat"] in the code, it is causing the code to perform the "if" and "else if" statements ONLY if the cat variable is set... so therefore if the variable is NOT set in the URL, then the "if" and "else if" statements never execute and therefore no error is produced. I hope this helps, if not, you are welcome to reply back and tell me what you're having trouble understanding.

Ker Sing Tan
Ker Sing Tan
10,572 Points

Thank you for detail explanation.