PHP has multiple functions to check PHP variables with respect to their initialized values. These functions are isset, empty and is_null.
I feel the effective way to communicate the difference would be with the help of a TRUTH TABLE,
“” | “apple” | NULL | FALSE | 0 | undefined | TRUE | array() | 123 | |
isset | TRUE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE | TRUE | TRUE |
empty | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE | FALSE | TRUE | FALSE |
is_null | FALSE | FALSE | TRUE | FALSE | FALSE | Warning / TRUE | FALSE | FALSE | FALSE |
difference.php
<?php
print "<br/>ISSET: <br/>";
$var = "";
print 'isset(""): ' . isset($var) . "<br/>";
$var = 'apple';
print "isset('apple'): " . isset($var) . "<br/>";
$var = null;
print "isset(null): " . isset($var) . "<br/>";
$var = FALSE;
print "isset(FALSE): " . isset($var) . "<br/>";
$var = 0;
print "isset(0): " . isset($var) . "<br/>";
print "isset(undefined): " . isset($var3) . "<br/>";
$var = TRUE;
print "isset(TRUE): " . isset($var) . "<br/>";
$var = array();
print "isset(array()): " . isset($var) . "<br/>";
$var = 123;
print "isset(123): " . isset($var) . "<br/>";
print "<br/>EMPTY: <br/>";
$var = "";
print 'empty(""): ' . empty($var) . "<br/>";
$var = 'apple';
print "empty('apple'): " . empty($var) . "<br/>";
$var = null;
print "empty(null): " . empty($var) . "<br/>";
$var = FALSE;
print "empty(FALSE): " . empty($var) . "<br/>";
$var = 0;
print "empty(0): " . empty($var) . "<br/>";
print "empty(undefined): " . empty($var1) . "<br/>";
$var = TRUE;
print "empty(TRUE): " . empty($var) . "<br/>";
$var = array();
print "empty(array()): " . empty($var) . "<br/>";
$var = 123;
print "empty(123): " . empty($var) . "<br/>";
print "<br/>IS_NULL: <br/>";
$var = "";
print 'is_null(""): ' . is_null($var) . "<br/>";
$var = 'apple';
print "is_null('apple'): " . is_null($var) . "<br/>";
$var = null;
print "is_null(null): " . is_null($var) . "<br/>";
$var = FALSE;
print "is_null(FALSE): " . is_null($var) . "<br/>";
$var = 0;
print "is_null('0'): " . is_null($var) . "<br/>";
print "is_null(undefined):" . is_null($var2) . "<br/>";
$var = TRUE;
print "is_null(TRUE): " . is_null($var) . "<br/>";
$var = array();
print "is_null(array()): " . is_null($var) . "<br/>";
$var = 123;
print "is_null(123): " . is_null($var) . "<br/>";
?>
Output:
ISSET:
isset(""): 1
isset('apple'): 1
isset(null):
isset(FALSE): 1
isset(0): 1
isset(undefined):
isset(TRUE): 1
isset(array()): 1
isset(123): 1
EMPTY:
empty(""): 1
empty('apple'):
empty(null): 1
empty(FALSE): 1
empty(0): 1
empty(undefined): 1
empty(TRUE):
empty(array()): 1
empty(123):
IS_NULL:
is_null(""):
is_null('apple'):
is_null(null): 1
is_null(FALSE):
is_null('0'):
Notice: Undefined variable: var2 in .../index.php on line 51
is_null(undefined):1
is_null(TRUE):
is_null(array()):
is_null(123):
isset:
Returns true for empty string, False, 0 or an undefined variable. Returns false for null.
empty:
Returns true for null, empty string, False, 0 or an undefined variable. Returns true if there is any value.
is_null:
Returns true for null only. Returns false in all other cases. Throws warning if the variable is undefined. If you suppress the warning, you will get true.
Value of $var | isset | empty | is_null |
---|---|---|---|
“” (empty string) | true | true | false |
‘apple’ (string value) | true | false | false |
null (declaration-only) | false | true | true |
FALSE | true | true | false |
0 | true | true | false |
undefined variable | false | true | Warning/true |
TRUE | true | false | false |
array() (empty array) | true | true | false |
123 (numeric value) | true | false | false |
Really appreciate the truth table.
Thank you!
Very Nicely Explained. Thanks.
Welcome Vishaal.
thanks for your clarification… I’ve learned a lot from it
Thank you.
Thanks for this article. It’s helpful, but there is a typo in this statement…
empty:
Returns true for null, empty string, False, 0 or an undefined variable. Returns true if there is any value.
The last sentence should read:
Returns false if there is any value.
Otherwise this article is pretty good. I like how you have code to print all the combinations to show and prove this is how php works and not just “take my word for it”.
One suggestion for the code: Add isset/empty/is_null(TRUE), this is helpful to see the difference between TRUE and FALSE.
Thank you Jeff, fixed it.
please check your “differences table” and the description for “empty” – empty(“apple”) for instance will not evaluate to true!
Thank you Dan for bringing to my notice. I have fixed it now. Thank you.
Thank you very much for this Great article
best regards
Thank you HMD.