It seems like there might be a few typos in your question, but I'll assume you're asking about comparing three PHP functions: empty(), is_null(), and isset(). These functions are used to check the status and value of variables in PHP. Let's discuss each function and their differences:
In summary:
- Use empty() to check if a variable is considered empty, including null.
$var1 = ""; // Empty string // empty($var1) => true
$var1 = "0"; // Empty string // empty($var1) => true
$var2 = null; // Null value // empty($var2) => true
$var3 = 0; // Numeric zero // empty($var3) => true
$var4 = array(); // Empty array // empty($var4) => true
- Use is_null() to specifically check if a variable is null.
$var = null; // is_null($var) => true
$var = 0; // is_null($var) => false
$var = array(); // is_null($var) => false
- Use isset() to check if a variable is set and not null.
$var = null; // isset($var) => false
$var = "Hello"; // isset($var) => true
$var = 0; // isset($var) => true
$var = array(); // isset($var) => true
Top comments (6)
Thanks for clearing the confusion Johnny
I personally prefer to use
empty()
to check if an array or any iterable type has any elements in itis_null()
to check if a varaible is null. I rarely useisset()
but comes very handy when checking a variable has been declaredThank you Given Ncube! !
I'm glad you found it helpful.
You can also add in your examples :
Clearly
Nice post it will help other dev who faced confusion between empty and isset.
Thank you Shshank!