Coding with Jesse

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

October 20th, 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.


Interested in web development? Subscribe to my newsletter!

Comments

1 . GM at 2008-10-22T06:17:56.000Z

GM

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 2008-10-23T18:00:41.000Z

ALM

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 2008-11-16T20:34:54.000Z

Damian

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 2009-02-04T01:21:06.000Z

lui

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 2009-02-04T09:49:53.000Z

John Griffiths

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

keep up the good work ;-)

6 . mar at 2011-05-08T00:58:44.000Z

mar

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

7 . bird at 2011-12-02T03:07:16.000Z

bird

thanks for sharing!!!

8 . Anay at 2012-04-30T04:24:37.000Z

Anay

Cool idea
Thanks

9 . Kumar at 2012-11-28T17:19:14.000Z

Kumar

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 2012-11-28T21:59:45.000Z

Jesse Skinner

@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.