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
Islamike Momen
Courses Plus Student 7,621 PointsDo we need the If statements if so why?
OK, I have just started watching and learning some PHP.
I'm currently up to here where professor Randy uses if statement (conditions) to determine whether a user have entered the URL with the correct shirt ID.
Here is the code that professor Randy uses:
<?php
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();
}
?>
Why do we need the first two if statement? Can't we replace the above code with following:
<?php
$product_id = $_GET["id"];
$product = $products[$product_id];
if (!isset($product)) {
header("location: shirts.php");
exit();
}
?>
Any enlightenment in this topic is appreciated.
Thanks.
4 Answers
Matt Campbell
9,767 PointsI'm not an expert but, my reckoning would be so that there's less calls to the database. First it's checking if there's a value to get, if no value is set then don't run a query on the database. Imagine if you had dozens of products and you ran queries for each and every value be it present or an empty value, it's unnecessary work on the database and server. Thus, slowing things down.
What does Randy say in the video?
Stone Preston
42,016 PointsThe query string could have been removed so you need to be sure the variable has been set before doing anything with it
Islamike Momen
Courses Plus Student 7,621 PointsI'm not sure what you mean. Please treat me like a newbie :D Thanks for talking your time to reply.
Stone Preston
42,016 PointsInstead of having ?Id=123 on the end of the URL (a get variable) The user could remove that, which means the variable would Not get set
Islamike Momen
Courses Plus Student 7,621 PointsUmm... This makes sense.
I'm going to try without the two condition and enter the url without the get variable and see what happens.
Thanks.
Islamike Momen
Courses Plus Student 7,621 PointsUmm... This makes sense.
I'm going to try without the two condition and enter the url without the get variable and see what happens.
Thanks.
Islamike Momen
Courses Plus Student 7,621 PointsWithout the first two If statement and invalid id, I still got redirected to shits.php when i entered.
I still wonder what steps they take in the background.
Thanks for your help
Patrick Castle
Courses Plus Student 14,591 PointsWith PHP it's a good practice to use if (isset()). You might not notice a difference depending on how your PHP interpreter is set up, but you can encounter notice messages on your web page complaining about missing indexes and undefined values.
In this case here - if(isset($_GET['id'])) might seem redundant because you think that 'id' may always exist as a value in the $_GET variable. You might even be right in this particular case. However, PHP pages are sometimes called multiple times using GET or POST, or GET with a different set of parameters. That means that there are going to be cases where the code you have there is run and the PHP interpreter will try to assess the value of $_GET['id'] when it doesn't necessarily exist.
I did a course recently where this came up and Notice messages are produced complaining about missing indexes. The code still actually runs OK, there's just extra junk on the screen. The instructor advised to change the settings of the interpreter so that those notices didn't appear. I don't particularly agree with this advice and think that code which generates warning messages from the interpreter is probably not good code and should be improved, but I can be fairly pedantic that way.
So - in summary. Leaving out some of these conditionals won't necessarily make a difference to the operation of the site, but it's good practice not to make too many assumptions based on input coming into a function/script. I wouldn't personally write the script the way it was presented. I'd probably write something like this:
<?php
$product_id = isset($_GET["id"]) ? $_GET["id"] : false;
if ($product_id && isset($products[$product_id])) {
$product = $products[$product_id];
header("Location: shirts.php");
exit();
} else {
/* something wrong with input/data */
}
>?
If I've tested that 'id' was an input parameter and I've established that there is a matching entry in the $products object, I shouldn't need to test isset($product).
Is any of this helping?
Islamike Momen
Courses Plus Student 7,621 PointsThank you.
Very detailed explanation, I appreciated.
Cheers
Islamike Momen
Courses Plus Student 7,621 PointsIslamike Momen
Courses Plus Student 7,621 PointsThanks for the reply.
Doesn't it query the database to see if it has a valid ID anyway? If not, where does condition checks if we have a valid ID?
If I'm not mistaken, the first and second if statement checks if we have a valid ID corresponding to the one that has been entered by the user.
Sorry for all these questions but I'm a complete newbie.
Thanks