PHP's isset() vs empty()

377 views, 1 reply
+1
0 / 1

isset() and empty() are often confused as inverses of each other, but that is NOT the case!

The following values are considered "empty" but also "set and not NULL":

empty(0); //returns TRUE
empty(''); //returns TRUE
empty(FALSE); //returns TRUE

isset(0); //returns TRUE
isset(''); //returns TRUE
isset(FALSE); //returns TRUE

But here is the difference between isset() and empty():

empty(NULL); //returns TRUE
empty("foo"); //returns FALSE
empty(5); //returns FALSE

isset(NULL); //returns FALSE
isset("foo"); //returns TRUE
isset(5); //returns TRUE

NULL is both "empty" and "not set."

A string of non-zero length and a non-zero number are both "not empty" and again "set and not NULL."

Hopefully that's clear now! Any questions?

Flag this comment as innapropriate
Posted 1 year ago by Dolph (120)
Post a reply anonymously:
+1
0 / 1

Good article. Just found another over at Tutorial Arena that takes a more in depth look at the difference between the two: <a href="http://www.tutorialarena.com/blog/php-isset-vs-empty.php">PHP isset vs empty</a>

Hope these 2 articles help someone.

Flag this comment as innapropriate
Posted 1 year ago by Anonymous (0)