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

    Tags: php syntax
    View 5 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 ;-)

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

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

    • jQuery Live Events
    • I need web developers
    • buttons need type="submit" to submit in IE
    • Win $200 in a Web Dev Writing Contest
    • 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 (39)
    • links (21)
    • about (19)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (6)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
  • Older Articles

    • 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 © 2010 The Future of the Web