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 trialLiban Shire
Courses Plus Student 4,147 PointsConfusing 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
1,850 PointsThe 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