How to Properly Use isset() in PHP

If you are wondering why isset() is returning false even though the variable or array key is clearly set, you are probably using it wrong. Learn how isset() actually works so you never use it wrong again! Plenty of PHP programmers are making mistakes with isset(), especially with HTML forms and database information.

What does isset() actually do?

The PHP default function isset() does two things. That’s right, it doesn’t just tell us if a variable or array key is set, even if it sounds like it should.

  • Checks if the it is set
  • Checks if the value is NULL

If the value is strictly NULL or the variable/array key is not set (declared), isset() returns FALSE! You cannot use isset() to see if a variable was declared. You must also be checking the value of the variable if you want to avoid using it wrong.

$variable = "hello";
if(isset($variable)){
 echo $variable;
} else {
 echo "The variable is not set.";
}

// The result of this code is the single word "hello" displayed on the page.

Everything looks fine, right? Nope! Consider the following, which is a great example of a developer not knowing the true logic behind isset()…

$username = NULL; 
if(isset($username)){
 if($username != NULL){
  // do something 
 } else {
  exit("The username cannot be blank.");
 }
} else {
 exit("The username variable is not set.");
}

If this appears normal to you or you are still unsure of isset()’s true functionality, keep reading. In this example above, the developer is first checking to see if $username is set. If it is, we are seeing if the username is not blank (!= NULL). It can be equal to NULL if the value is empty / blank, or strictly equal to NULL or “”. So, having this type of logic can still be good because it does check for a username that is sent as not strictly (===) equal to NULL (example: == ”).

The problem is that the developer is assuming that $username variable is not set simply because isset() returned false. Remember, isset() will return FALSE if the username is equal to NULL, so the error reporting is not correct. Here is how we can fix it…

$username = NULL; 
if(isset($username)){
  // the $username variable is available to analyze AND it is not STRICTLY === equal to NULL
  // it may still == '' (blank), so we can analyze length, validity, etc here
} else if($username === NULL){
 // The username is === to NULL, but we do still have a $username variable being set 
} else {
 /* We know the username variable is not set (because the else-if above covers NULL values), which may indicate a routing or data error 
 (Please remember that sometimes these if statement blocks are not directly below the 
 variable being set, as they are dynamic to a database or array) */
}

The problem with isset() is that a lot of times developers simply do not catch the mistake and only use a single if and single else statement to analyze. This is a problem when NULL is a default value in a database, or something is set to be NULL.

Here’s one more example…

if($_GET['id']){
 $pagetitle = getPageTitle($_GET['id']);
}
if(isset($pagetitle)){
 echo "The page is $pagetitle";
} else {
 echo "The page ID must be supplied to load this resource.";
}

The problem with this is that it is assuming that $pagetitle will be set as long as $_GET[‘id’] is there. However, what’s really happening is this…

  1. If $_GET[‘id’] is supplied, set the variable $pagetitle equal to the result of a function to fetch the page title
  2. Check if $pagetitle is set… Or NOT NULL.
  3. If !isset($pagetitle), display an error that the page ID must be supplied.

However, if the getPageTitle() function returns a value of NULL for that page ID, the code below will supply the wrong error message. After all, the page ID was supplied – it just didn’t fetch an expected (probably string) page title.

Here’s the fix:

if($_GET['id']){
 $pagetitle = getPageTitle($_GET['id']);
} else {
 echo "The page ID must be supplied to load this resource.";
}
if($pagetitle != NULL){
 echo "The page is $pagetitle";
} else {
 echo "This page does not have a title.";
}

As you can see, we have done some rearranging to make sure that the error messages supplied are actually accurate. This may seem trivial and like there doesn’t need to be a giant article about the mistake of using isset() wrong, but it happens enough that people need to learn the proper way to use it.

Learn PHP Online for Free

If this article helped you, please consider subscribing to Nerdi for more tutorials, articles, and lists of resources, libraries, and other programming related things for you to explore. If you want to discuss technology, coding, and more check out Code Forum – my favorite online community for programmers!