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 trialSaad Aleem
6,089 PointsWhy use isset at all?
I mean if(isset["$_GET["id"]) and if($_GET["id"]) are giving me the same results. This is true for 2 of the 3 occurrences of isset.
So what exactly is the point of using it then?
2 Answers
Andrew Shook
31,709 PointsIn the example above, if you don't use isset() you will receive an undefined index warning if $_GET['id'] isn't passed by the url. Not a big deal, but it's not considered best practices to intentionally have error's or warnings on a production site. Also, the url could look like this, " www.example.com/?id=". In this case, isset() would tell you if the variable actually has a value. By using isset() to check if a value, you can stop processing if there is no value.
Robert Mews
11,540 PointsDepending on your php preferences, you may not be receiving an error for when someone enters an unknown shirt ID into the header. The reason for the isset function is to remedy any error the user may have encountered when navigating to a shirts page. We typically want the user to avoid pages in which there is no content available to display (i.e. navigating to a page that there isn't a shirt ID).
My question, is why redirect the user to the shirts.php page? Why not create a conditional in which the user receives a message on the shirt.php page that says "sorry that shirt does not exist" something akin to a 404 error page?
Saad Aleem
6,089 PointsSaad Aleem
6,089 PointsI'm actually talking about the shirt.php template associated with this video. I didn't get an undefined index warning, maybe because of the else condition.
Don't both if(isset($_GET["id"]) and if($_GET) mean "if $_GET["id"] is not equal to 0"? Since both return "true" in this case. If not, what exactly is the difference between these two?
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsNo they do both mean "if the value is not equal to zero". isset() means does this variable exist and does it not equal zero. You will still get a warning if you check for a variable that doesn't exist. I.e "shirts4mike.com/shirts/?" will cause a PHP warning because the GET variable "id" isn't set.