Empty() and Isset() in PHP

This is just a quick tutorial regarding the empty() and isset() functions for people that are fairly new to the world of PHP programming. I learned this the hard way a long time ago while I was working on my first few form processors in PHP.

From the PHP Web site, referring to the empty() function:

Returns FALSE if var has a non-empty and non-zero value.

That’s a good thing to know. In other words, everything from NULL, to 0 to “” will return TRUE when using the empty() function.
Here is the description of what the isset() function returns:

Returns TRUE if var exists; FALSE otherwise.

In other words, only variables that don’t exist (or, variables with strictly NULL values) will return FALSE on the isset() function. All variables that have any type of value, whether it is 0, a blank text string, etc. will return TRUE.

Something else you need to know is that textareas and textboxes in forms will be sent with “” values rather than NULL values to the $_POST[] array.

Therefore, if you are trying to process a form, and you want to make sure the person entered something into the field, you are much better off checking to see if the form value is empty, than you are checking to see if it isset(). On the other hand, if you are trying to check the value of a radio button or combobox in which one of your possible values is “0”, then you should be using the isset() function.

For the first few form processors I worked on, I was consistently using the following code:

if(isset($_POST[myField]) && $_POST[myField] != "") 
  //Do my PHP code

A more efficient way of doing this is obviously to use:

  //Do my PHP code

Just a quick tip for people struggling with this. There are two main points in this article:

1) isset() and empty() are not exactly opposite of each other. They actually check for two very different things. Therefore, using !isset() is not the same as using empty(), nor is “!empty” the same as using isset(). That’s a good thing, but it’s not something you generally figure out right off the bat.

2) Forms will send blank values to the $_POST[] array instead of sending NULL values. Therefore, the variable you are usually checking from your form is, in the strictest sense of the word, “set”. However, it may still have an “empty” value. Therefore, if you really only want to check to see if the variable exists, even if it has an empty value (or if you want to check if a variable doesn’t exist), then you want to use the isset() function. However, if you want to check to see if the variable has (or doesn’t have) an empty value, then you are better off using the empty function.

I hope that helps someone out there avoid some of the coding mistakes I made early in the game. Enjoy.

30 Responses

  • Mary

    Thanks for the clarification. I, too, had to learn this the hard way, but now I clearly understand my mistake.

  • Mohammad Hussein

    Thank you.
    I had a confusing bug and your article made it clear.

    • chris

      This is why isset() is mainly used to check to see if a “hidden” input is set (and thus is a good way to activate php code upon submission of a form).

  • Daniel

    Wow, you’ve helped me alot. Thank you!

  • Jeffrey

    Hi I want to check a number of fields to ensure that they are not left empty. Your tutorial above was a great help to me to understand how to check a field. Is there a method for checking a group of fields at one time or should I check each field individually.

    Thanks for the help.

    • please help me give me a sample code in php empty…
      the scenario is i want to tell that if the field of the table is empty the result is no records found if the record has a data it will select the data

  • @Jeffrey
    To check multiple fields at once, you can use either PHP’s AND syntax as follows:

    if(!empty($_POST['field1']) && !empty($_POST['field2']) && !empty($_POST['field3']))
    // Do whatever you like here

    If your script is built with error handling, to post back errors, you’re going to want to do this all separately, so that you can send back distinctive error messages:

    session_start(); // I set my scripts with sessions
    $form_err = array(); // Setup an array for error storage
    $form_flag = FALSE; // Preset to FALSE to show it started with 0 errors
    if(!empty($_POST['field1'])) {
    $form_err[] = 'Field empty';
    $form_flag = TRUE;
    if(!empty($_POST['field2'])) {
    $form_err[] = 'Field empty';
    $form_flag = TRUE;
    if($form_flag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    header("location: your/page/goes/here");

    Then just simply work your way through the errors to for the user, so they know exactly what they did wrong.

  • Whoops,

    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;

    should actually be

    $_SESSION['ERRMSG_ARR'] = $form_err;
  • This is a good tip, but it’s important to remember that if a user that submits a single blank space in a form field, that field will be set and not empty.

    The following code will echo “not empty”.

     $bob = " ";
         echo "empty";      
        } else {
          echo "not empty";

    I use trim() almost to an insane extent to catch these cases. You’d be surprised how many forms authored in PHP will accept a single blank space as valid input.

  • I am just beginning to learn PHP, HTML, and SQL.
    I have started writing a PHP program and am having trouble. I have a user input information on a form which I am trying to test using the empty or isset commands but I am not having any luck. I have the variables named as follows:
    Input Form: easypick is a radio button that either is a Yes or No responce (returns Y/N). firstnumbers is a checkbox that allows mulitple entry of numbers from (1-56).
    When I go to check to see if the user wants a random number generated or if he has selected the
    numbers, I am checking these values with the following code below, but I am not getting the correct response. What am I doing wrong?

    // Check to see if the user wants to have the computer randomly select his  numbers.
    if  ((empty($_POST['firstnumbers[]'])) && (!empty($_POST['easypick'])) && ($easypick ="N")) 
      echo 'User does not want to random select his numbers';
    else if ((empty($_POST['firstnumbers[]'])) && (!empty($_POST['easypick'])) && ($easypick ="Y")) 
      // Ok, User wants to have his numbers randomly selected and did not choose any of the first five  numbers.
      echo 'Inside Here - User Want to Random Select His Numbers';
  • Laura Aspen

    Thanks Curtis! Your clear explanation was very helpfull!

  • richard

    hey thanks for the empty function.

    A lot of people recommended me to use isset but it wasn’t always working.

    Anyway the empty() approach is way better for the forms.

  • Craig

    What do you suggest when the submitted value should not be blank but 0 is acceptable. For example a form that may request the number of children a person has. In this case neither isset or empty would work. I guess you could work something out with is_numeric but when validating with loops I don’t really see much of an alternative to isset && !=””. Any thoughts?

    • I’ve run into that problem, myself, before, too. Although it’s a little complicated, I think I usually end up using something like

      if( isset( $var ) && ( !empty( $var ) || 0 == $var ) ) )

      I’ll try to remember to take a look at some of my old code and see if I can find a better solution.

  • Stephanie

    Thank you so much! This article saved me SO much time debugging :)

  • Kate

    Thanks! This is helpful and saves me.

    • Allen

      Good to hear this post helps!

  • Satyanarayan

    Timeless classic advice. Should be on Headlines of PHP books since form validation is very basic. Books on PHP gave the wrong codes…But your tut’s explained this well.

  • Agree, many PHP books (and not only PHP) tend to give you a standard “Hello World” type of learning curve.

  • Rash

    Really helpful article with proper clarification. Thanks a lot.

  • amit

    thank you ..
    it helped me !

  • Guest

    whoop .. thank you , you have no idea how it helped me . haha beginner here

    • good to hear. we are all beginners in one way or another :)

  • Muhammad Shoaib


  • ridhyik banerjee

    I have a html contact form. Input values are shown in a php script and store it in email via ’email() in php’. But when direct url of php page is pressed on address bar a empty mail is sent. I want to run the php page only when the variables are collect from my html form and no direct url access. please help me in any way….
    Email: [email protected]

  • blacmoon

    Thanks author…!

  • ezzzy

    Wow. This helped my life, for days is have been battlin with isset() instead of using empty()

  • sjambok

    I am using php in Actionstep. I want to manipulate a first date field that could be blank or have a date in it (its a drop down type field). If a date has not yet been selected in such a date field, is it empty, or would it be regarded as the earliest date (1 Jan 1970)? I want to write code that states “blah” if no date has been selected on the original date field, and calculates a second date from the first date field if a date has been selected (say 2 months ahead).

    I have tried this (without success):

    $date = '[[Important_dates_Date_of_Direction_to_Request_Exam]]'; 
    $calcdate = date('d F, Y', strtotime('+2 months', strtotime($date))); 
    if (empty($date)) {
        echo 'No direction received yet';
    else {
        echo $calcdate;

    what am I doing wrong?

    • Your declaration of $date is wrong (I am assuming it’s value is a part of a submitted form).
      If the form is submitted via a POST request method, then you should use:

      $date = $_POST['Important_dates_Date_of_Direction_to_Request_Exam'];

      If it is a GET request method, then you should use:


      If you’re not sure which method is used, you can use isset() and empty() to check if it is set, or just use:


      NOTE: $_REQUEST contains all values in $_GET, $_POST and $_COOKIE, so it is not advisable to use in a production code.

  • In PHP, there are built-in methods for finding whether an array is empty or not. It is the developer’s choice for what function he/she will opt for checking the array. It is all about their requirements and preferences.