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

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

If else statement sequence

Hello,

So I have another question about shirt.php, I'm not quite sure I understand the if else statement sequence. I will write comments on my code as I understand the sequence please correct me if I'm wrong or I am missing something.

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

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

} // How does the code complete the if statement here for both IF ISSET statements when there is another if statement within the product id ISSET. Does the second if statement run after completing or it stops at GET ID, how does it work, I'm confused.

Thank you,

Giovanni

4 Answers

If $_GET["id"] is NOT set, it will fall to else, that means it will display "We have no shirt ID".

If $_GET["id"] is set it will pass the $_GET["id"] value to $product_id and after that it will check if $products["products_id"] is set. If $products["products_id"] is set it will pass the value of "$products["$products_id"]" to "$product" variable and after that it will display "We have a valid shirt ID".

If $products["products_id"] is NOT set it will display "We have a shirt ID, but is is invalid."

If the first if (if isset $_GET['id']) is true, it will check the second one if not, it will go on with the next code

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Thank you got it. The whole 2 ifs within each other just confused, but I understand now.

Can you check the answer as best answer pls?

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

OK I kind of understand my main questions I put under comments by the code. Can you please help me out explaining thanks. What confusing me the most is the 2 if statements within each other.

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

if(isset($products["products_id"])) {   // DOES THIS code run at all if GET ID ISSET Is not true??? 
    $product = $products[$product_id]; 
    echo "We have a valid shirt ID";     
}  

else { echo "We have a shirt ID, but is is invalid."; }

} else { echo "We have no shirt ID.";} // WHY do he curly braces of the first ISSET end here.

exit();