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

Konrad Pilch
Konrad Pilch
2,435 Points

Im confused

HI, what does this code mean? or rather, how does it work? like the isset stuff and the if nested in if and another if like thats confuing

if (isset($_GET["id"])) {
    $product_id = $_GET["id"];
    if (isset($products[$product_id])) {
        $product = $products[$product_id];
    }
}
if (!isset($product)) {
    header("Location: shirts.php");
    exit();
}

3 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Konrad, 'isset' is used to determine that a value is not null.

// we need to get $_GET["id"],
// but only if its not null
if (isset($_GET["id"])) {
    // store it
    $product_id = $_GET["id"];
    // check if $products[$product_id] is not null
    // before using it
    if (isset($products[$product_id])) {
        $product = $products[$product_id];
    }
}
// redirect if $product wasn't successfully created
if (!isset($product)) {
    header("Location: shirts.php");
    exit();
}

Does this help?

Joshua Edwards
Joshua Edwards
52,175 Points

It is checking to see if that form parameter has been set. So it is saying if the form field id is set to a value assign that value to a variable product id. Then it essentially does the same for the next part it just uses the product id variable to look up which product it is then set the product variable to whichever product it is in the products array.

Konrad Pilch
Konrad Pilch
2,435 Points

So basicaly isset is checking if the product_id od Id is there and if it is, it makes the product as products id and displays the right id from the shirt in the page right?

Damien Watson
Damien Watson
27,419 Points

Basically, yep. Isset is an easy way of checking for valid values. :)

Konrad Pilch
Konrad Pilch
2,435 Points

Oh okay, i get it better now :) thank you everyone for answer :)