[ACCEPTED]-PHP: Variable empty or not set or what?-conditional
Check PHP manual out: http://www.php.net/manual/en/types.comparisons.php
Expression gettype() empty() is_null() isset() if($x) $x = ""; string TRUE FALSE TRUE FALSE $x = null; NULL TRUE TRUE FALSE FALSE var $x; NULL TRUE TRUE FALSE FALSE $x undefined NULL TRUE TRUE FALSE FALSE $x = array(); array TRUE FALSE TRUE FALSE $x = false; boolean TRUE FALSE TRUE FALSE $x = true; boolean FALSE FALSE TRUE TRUE $x = 1; integer FALSE FALSE TRUE TRUE $x = 42; integer FALSE FALSE TRUE TRUE $x = 0; integer TRUE FALSE TRUE FALSE $x = -1; integer FALSE FALSE TRUE TRUE $x = "1"; string FALSE FALSE TRUE TRUE $x = "0"; string TRUE FALSE TRUE FALSE $x = "-1"; string FALSE FALSE TRUE TRUE $x = "php"; string FALSE FALSE TRUE TRUE $x = "true"; string FALSE FALSE TRUE TRUE $x = "false"; string FALSE FALSE TRUE TRUE
As you can see, if(!empty($x))
is 3 equal to if($x)
and if(!is_null($x))
is equal to if(isset($x))
. As far as if 2 $data != ''
goes, it is TRUE
if $data
is not NULL
, ''
, FALSE
or 0
(loose 1 comparison).
if (isset($data)) {
Variable is just set - before that line 10 we declared new variable with name 'data', i.e. $data 9 = 'abc';
if (!empty($data)) {
Variable is filled with data. It 8 cannot have empty array because then $data
has 7 array type but still has no data, i.e. $data 6 = array(1);
Cannot be null, empty string, empty 5 array, empty object, 0, etc.
if ($data != '') {
Variable is 4 not an empty string. But also cannot be 3 empty value (examples above).
If we want 2 to compare types, use !==
or ===
.
if ($data) {
Variable is filled 1 out with any data. Same thing as !empty($data)
.
They aren't the same.
true if the variable 8 is set. the variable can be set to blank 7 and this would be true.
true if the variable 6 is set and does not equal empty string, 0, '0', NULL, FALSE, blank 5 array. it is clearly not the same as
isset
.if 4 the variable does not equal an empty string, if 3 the variable isnt set its an empty string.
if 2 the variable coerces to true, if the variable 1 isnt set it will coerce to false.
if (isset($data)) - Determines if a variable 8 is set (has not bet 'unset()'
and is not NULL
.
if (!empty($data)) - Is 7 a type agnostic check for empty if $data 6 was '', 0, false, or NULL it would return 5 true.
if ($data != '') { this is a string 4 type safe of checking whether $data is not 3 equal to an empty string
if ($data) { this 2 is a looking for true or false (aka: 0 or 1 1)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.