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

Liban Shire
PLUS
Liban Shire
Courses Plus Student 4,147 Points

Confusing code when setting $_GET["id"] and $product

I have an understanding of what this code really does but the code isself confuses me I would appreciate if someone can go over it line by explaining it thanks a lot.

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

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

}

1 Answer

Tom Schouteden
Tom Schouteden
1,850 Points

The two lines containing the isset() function, are making sure there's an actual value in the variables ($_GET["id"] and $products[$product_id]). Otherwise PHP might throw a warning when you are assigning the variable because it's empty.

The second line is assigning the contents of $_GET["id"] to $product_id. The fourth line is assigning the right product from the $products[] array to the $product variable.

Finally, if no product was assigned to the $product variable, PHP will redirect your browser to the shirts.php page.

Hope this helps

tom