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 on October 22nd, 2008

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 on October 23rd, 2008

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 on November 16th, 2008

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 on February 3rd, 2009

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 on February 4th, 2009

John Griffiths

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

keep up the good work ;-)

6 . mar on May 7th, 2011

mar

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

7 . bird on December 1st, 2011

bird

thanks for sharing!!!

8 . Anay on April 30th, 2012

Anay

Cool idea
Thanks

9 . Kumar on November 28th, 2012

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 on November 28th, 2012

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.

Comments are closed, but I'd still love to hear your thoughts.