Hey,
This post is all about difference between empty() and isset().
In PHP empty() and isset() is two in-built function, empty() is used for check whether the variable is empty or null and isset() check whether a variable is defined or not. Here below I have listed some important information which could gives you idea about the these two functions.
Case -1:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $var = 0; // True because $var is empty if (empty($var)) { echo "Variable 'var' is empty.<br>"; } // True because $var is set if (isset($var)) { echo "Variable 'var' is set."; } ?> |
Case-2:
1 2 3 4 5 6 7 8 9 10 11 | $var = NULL; // True because $var is empty if (empty($var)) { echo "Variable 'var' is empty.<br>"; } // False because $var is NULL if (isset($var)) { echo "Variable 'var' is set."; }else{ echo "Variable 'var' is not set."; } |
Similarly, You can now see isset() and empty() returns true or false in different conditions.
Value of variable ($var) | isset($var) | empty($var) |
---|---|---|
“” (an empty string) | bool(true) | bool(true) |
” ” (space) | bool(true) | bool(false) |
FALSE | bool(true) | bool(true) |
TRUE | bool(true) | bool(false) |
array() (an empty array) | bool(true) | bool(true) |
NULL | bool(false) | bool(true) |
“0” (0 as a string) | bool(true) | bool(true) |
0 (0 as an integer) | bool(true) | bool(true) |
0.0 (0 as a float) | bool(true) | bool(true) |
var $var; (a variable declared, but without a value) | bool(false) | bool(true) |