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

Ker Sing Tan
Ker Sing Tan
10,573 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() ?

2 Answers

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,573 Points

Thank you for detail explanation.

Jon Guest
Jon Guest
1,532 Points

Very good answer.

KEY TAKEAWAYS

"When the instructor adds ?cat="music" to the HTML , this is what is setting the variable of "cat"."

( <a href="catalog.php?cat=music"> an unusual place to name a variable )

Really good BRAIN TWISTER for beginners: Here we need to decide. Should we let a program run our code block if it produces an error. We decide to let it run only if the conditions are met.

if ( isset ($_GET["cat"])) {

  • write code you want to run
  • if () checks with isset() if $_GET[] has variable ?cat created }