the Future of the Web
  • Articles
  • Contact
  • 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.

    View 10 Comments | Add a comment
  • 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.

  • Jesse Skinner

    Jesse Skinner
    • About Me
    • Email Me
    • RSS Feed RSS Icon
    • @JesseSkinner
  • Recent Articles

    • Free eBook: Unobtrusive Ajax
    • Official jQuery Templating Plugin
    • jQuery Live Events
    • buttons need type="submit" to submit in IE
    • Use Arrays in HTML Form Variables
    • 5 Reasons Freelancers Can Succeed in a Shrinking Economy
    • Keeping a Live Eye on Logs
    • Using PHP's empty() Instead of isset() and count()
    • Testing Web Pages with Lynx
    • Stop CSS Background Flickering in Internet Explorer 6
    • See All...
  • Categories

    • javascript (41)
    • about (17)
    • links (17)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (5)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
  • Older Articles

    • October 2010
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2013 The Future of the Web