• Using PHP's empty() Instead of isset() and count()

    Oct 20 2008

    I often work with data arrays in PHP as a way to pass information around or store information in sessions. When you work with these, you can't always assume that all properties are defined. I had some conditional logic code in PHP that was only supposed to execute if an array contained any values:

    $data = array(
       'text' => array( 'hello', 'world' ),
       'numbers' => array( 43, 2, 55 )
    );
    
    if (count($data['text'])) {
       // do something with $data['text']
    }
    

    But then I was in a situation where $data['text'] may or may not be defined. So I was going to update my if statement like so:

    if (isset($data['text']) && count($data['text'])) {
       // do something
    }
    

    But that looks kind of messy. I don't really like isset() but it is a necessary evil to avoid "Undefined" errors. Or is it?

    if (!empty($data['text'])) {
       // do something
    }
    

    empty() to the rescue - it returns true if $data['text'] is undefined, or if it is an empty array, or if it is false or null or 0. So !empty() is what I'm really trying to determine, and it works great.

    For more info, see: empty() at PHP.net.

  • Comments

    1. GM at 2:17am on October 22, 2008

    Only you have to remeber to use isset for values like 0. I have had problems when trying to validate form select element with "0" value. :)

    2. ALM at 2:00pm on October 23, 2008

    really?
    isset($_GET['var1']) returns false , if the querystring (or method=post equivalent) is say, http://mysite/index.php?var1=0  ?

    Or what?  Just curious...

    empty() doesn't enter my thoughts very often, good reminder.  You rekindle my dream to blog through all PHP functions from A to Z :)

    3. Damian at 3:34pm on November 16, 2008

    Great advice Jesse.
    Like GM say, we have to be careful when we have an "0" value and we check for !empty. A "0" value will be return a true value for empty and generally if you have "0" it represents that you have a value.

    4. lui at 8:21pm on February 3, 2009

    I am currently testing my scripts with functions such as empty(), eregi(), str_length(). But at the end null values and values of not the same expected string length are easily inserted into my database. i.e

    function ur()
    {
    if(empty($_POST['a']))
    {
    print "somthing";
    return;
    }

    5. John Griffiths at 4:49am on February 4, 2009

    thanks for your post, these things can get annoying fast.  helped a lot.

    keep up the good work ;-)

    6. mar at 8:58pm on May 7, 2011

    http://www.php.net/manual/en/function.empty.php#103756
    solves the "0" problem.

    7. bird at 10:07pm on December 1, 2011

    thanks for sharing!!!

    8. Anay at 12:24am on April 30, 2012

    Cool idea
    Thanks

    9. Kumar at 12:19pm on November 28, 2012

    I have had this problem where empty would not be a sufficient check. So if I am querying the DB and the resultset is empty the return value is false. empty() took 0 as a value and returned true ...

    10. Jesse Skinner at 4:59pm on November 28, 2012

    @Kumar - that's correct, empty(0) is true, so it's not good if you're checking for a null value. You may want to check against null, the empty string "" or false, depending on your database setup.

    Commenting is now closed. Come find me on Twitter.