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 Simple PHP Application Integrating with PayPal Redirecting Invalid Shirt IDs

Ian Hilton
Ian Hilton
6,223 Points

How does isset check to see if a variable is loaded before the the code loads a variable ?

I've watched the video 5 times now. This stuff is starting to make me feel like part of my brain is missing. I need some help understanding what is happening here. I understand the paradigm of if-then-else statements. What I don't get is, how does isset work? How do you check for id when id is set in the following statement? I'm not even sure I know what question to ask. Any help understanding is really appreciated.

<?php include("inc/products.php");

if (isset($_GET["id"])) {
$product_id = $_GET["id"];
if (isset($products[$products_id])) {
$product = $products[$product_id];
}
}

if (isset($product)) {
header("Location: shirts.php");
exit(); }

1 Answer

Jeremy Hayden
Jeremy Hayden
1,740 Points

Good question!

If you notice in your code, there are two ID's

There is $_GET["id"]. This is being returned to the browser from the request sent out.

The isset is asking "did we get an answer?" If so, make our local variable $product_id equal that.