3 ms·
EDIT: Brain fart; don't use this. See comments below. Just use the following if you really want to account for strings containing the number 0. (!isset($v
by rmrfrmrf 12y ago
EDIT: Brain fart; don't use this. See comments below.
Just use the following if you really want to account for strings containing the number 0.
(!isset($var) || $var === false);
Note the triple equals sign.
- arenaninja 12y agoThis won't work. Both 0 (numeric) and '0' (string) == false, but never === false (because when you === you're checking that it's the same type). If you expect a string '0', simply check strlen($str) > 0. If the value could be numeric, use a (string) cast.
- rmrfrmrf 12y agoYou're right; my bad. I'll edit if possible.
- wvenable 12y agoIf you really want to test for empty strings where $a is defined just use: if ($a == "") { ... } Empty is merely the function equivalent of the not (!) operator. The semantics are exactly the same except for the handling of undefined variables. It's not something one should use except if you're expecting to deal with undefined variables.
- chriswarbo 12y ago> Empty is merely the function equivalent of the not (!) operator. Nope! "empty" is an operator too: var_dump(array_map('empty', [null, '', 5, false])); PHP Warning: array_map() expects parameter 1 to be a valid callback, function 'empty' not found or invalid function name NULL See https://bugs.php.net/bug.php?id=66368 https://bugs.php.net/bug.php?id=66368