note 29712 modified in function.empty by philip
| From: | philip@php.net | Date: | Wed, 26 Mar 2003 01:02:17 +0000 |
| Subject: | note 29712 modified in function.empty by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-45825@lists.php.net to get a copy of this message | ||
[[[Editors note: It depends on what a person wants and it's not uncommon for someone to want
'0' to be seen as empty. In PHP3, the string '0' was not considered empty.
Anyway, doing $foo == '' is an option but it will shout an E_NOTICE level error if $foo is
not set. strlen() would also provide this E_NOTICE error, as would if($foo).]]]
I do not use empty() nor isset() to check form input. When I want to know if some field has been
filled in, I simply check if $field!=''. My reasons are:
-empty() fails if the field has 0 (the zero value), which may be a value I need.
-isset() always returns true for a POST var, even if the form field was empty.
So instead of...
if (!empty($field)) {...} // bad
...or...
if (isset($field)) {...} // bad
...I use
if ($field != '') {...} // good!
As other comments here, I also believe empty() would be more use useful if it returned true for 0.
--was--
I do not use empty() nor isset() to check form input. When I want to know if some field has been
filled in, I simply check if $field!=''. My reasons are:
-empty() fails if the field has 0 (the zero value), which may be a value I need.
-isset() always returns true for a POST var, even if the form field was empty.
So instead of...
if (!empty($field)) {...} // bad
...or...
if (isset($field)) {...} // bad
...I use
if ($field != '') {...} // good!
As other comments here, I also believe empty() would be more use useful if it returned true for 0.
http://www.php.net/manual/en/function.empty.php