21

What is the difference between !isset and empty, isset and !empty ??

Spudley
  • 166,037
  • 39
  • 233
  • 307
yumugee
  • 993
  • 3
  • 9
  • 13
  • 2
    http://virendrachandak.wordpress.com/2012/01/21/php-isset-vs-empty-vs-is_null/ first hit at google "php isset vs empty" – clentfort Jun 03 '12 at 13:23
  • possible duplicate of [isset() or !empty() functions on all variables in your views? PHP](http://stackoverflow.com/questions/1938061/isset-or-empty-functions-on-all-variables-in-your-views-php) – mario Jun 03 '12 at 14:03
  • 1
    or [Why check both isset() and !empty()](http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty) – mario Jun 03 '12 at 14:03
  • The link in the first comment for "php isset vs empty" has been updated to http://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ – Virendra Aug 16 '14 at 00:43

2 Answers2

17

The type comparison tables should answer all questions about these operators: http://php.net/manual/en/types.comparisons.php

mjhennig
  • 671
  • 5
  • 11
  • I didn't realize that `$x = "";` and `$x = null;` produced different results. That's handy information to have. – doubleJ Jan 28 '13 at 20:16
2

The basic answer would be that a variable can be set (not NULL) and yet be empty(can be assimilated to 0). Consider an empty array for example.

From the link presented by @mjhennig, you can see that 0 is considered to be empty. So is False and the empty string(obviously) :)

Samy Arous
  • 6,794
  • 13
  • 20