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 PHP & Databases with PDO PDO Database Security Cleanup & Final Steps

Seth Warner
Seth Warner
5,348 Points

why didn't he just add a else section on the original if(!empty($_GET['id']) instead of messing with an isset, and

and adding a if true on the fetch?

3 Answers

Tommy May
Tommy May
12,056 Points

Seth Warner, there are tons of ways to solve the same problem. It is common to see developers check for both

if (!empty($var) && isset($var) ) { // Do code }

One big reason to use isset() AND !empty is because !empty will not throw you a notice in your php error log. These notices can be useful when debugging so isset() is good for that.

Seth Warner
Seth Warner
5,348 Points

interesting, could he of just written an else for the if statement and put an exception there with a string starting there was a error as well?

Tommy May
Tommy May
12,056 Points

Yeah that is one way to go about it. The isset will basically accomplish the same thing but if you throw in the else statement your suggesting, then you will have control of what the error message will output rather than the standard error message isset will generate

Although - I would write this the other way around because conditionals get checked left -> right :)

<?php

isset($var) && !empty($var)

otherwise you'll get an error thrown on the first empty check because the $var you're checking isn't set.

Generally speaking, you probably own't need to use the empty function. If it's empty (hasn't been defined a type) or has a type, but that type is empty (like an empty array) it will return a boolean false - so you can shorten to:

<?php

isset($var) && $var

// i.e. it is set, and there's something sitting inside it with a value.

Come to think of it, I haven't found a need to use 'empty' since I started!