I don't understand the difference between isset() and !empty() . Because if a variable has been set, isn't it the same as not being empty? asked Dec 14, 2013 at 11:56
VitalynxVitalynx 9061 gold badge8 silver badges16 bronze badges 3 ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not
assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE for ISSET . EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.
Andrew 2673 silver badges11 bronze badges answered
Dec 14, 2013 at 11:59 NambiNambi
11.8k3 gold badges36 silver badges49 bronze badges 4 And one more remark. empty() checks if the variable exists as well. I.e. if we perform empty() to the variable that wasn't declared, we don't receive an error, empty() returns 'true'. Therefore we may avoid isset() if next we need to check if the variable empty. So isset($var) && !empty($var)
will be equals to !empty($var)
answered Jul 5, 2017 at 10:04
Andrey P.Andrey P. 1192 silver badges14 bronze badges Use !empty when there is a condition already present which is checking for true or false.
isset is more basic. empty incorporates more checks, hence needs to be used with care. answered Sep 15 at 3:39 CodeForGoodCodeForGood 7576 silver badges20 bronze badges isset — Determine if a variable is set and is not NULL.
!empty — Determine whether a variable is NOT empty.
answered Dec 14, 2013 at 11:58
2 Isset return false if variable has not been set or it is null and return true if variable has been set and not null. !empty return
true if variable has been set and not empty. Empty string, empty array, "0",0 and false are defined as empty. answered Dec 14, 2013 at 13:14 by Vincy. Last modified on September 15th, 2022. PHP has multiple functions to check PHP variables with respect to their initialized values. These functions are isset, empty and is_null. - isset() is to check if a variable is set with a value and that value should not be null.
- empty() is to check if a given
variable is empty. The difference with isset() is, isset has null check.
- is_null() is to check whether a variable is defined as 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
|
Ok! Now find the difference!Code to understand the differencedifference.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):
Differences
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. Differences table
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
|
References- isset PHP manual
- empty PHP manual
- is-null PHP manual
↑ Back to Top
Does Isset check for empty string?
"isset() checks if a variable has a value including (False, 0 or empty string), but not NULL.
Is NULL vs Isset PHP?
PHP isset() vs.
The is_null() function returns true if the value of a variable has been explicitly set to NULL . Otherwise, it simply returns false . On the other hand, isset() will return true as long as a variable is defined and its value is not NULL .
Does Isset check for empty array?
empty() function in PHP ? The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.
What is NULL and empty in PHP?
A variable is NULL if it has no value, and points to nowhere in memory. empty() is more a literal meaning of empty, e.g. the string "" is empty, but is not NULL . The following things are considered to be empty: "" (an empty string) 0 (0 as an integer)
|