note 78596 added to function.empty
| From: | BigBadaboom at osu1 dot php dot net | Date: | Thu, 18 Oct 2007 15:44:40 +0000 |
| Subject: | note 78596 added to function.empty | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131505@lists.php.net to get a copy of this message | ||
If you are writing a form handling script and just need to check whether a required field has been
filled in, you can just use strlen() to check the field. It handles all the cases properly when you
are only dealing with strings.
$x = "0";
echo strlen($x); // returns 1
$x = "";
echo strlen($x); // returns 0
unset $x
echo strlen($x); // returns 0
In other words, the check you need to make becomes:
if (strlen($email) == 0)
$error = "Please enter your email address";
or
if (strlen($_POST["email"]) == 0)
$error = "Please enter your email address";
Hopes this helps anyone just seeking a workaround for the annoying empty("0") anomaly.
----
Server IP: 203.89.181.186
Probable Submitter: 121.72.242.206
----
Manual Page -- http://www.php.net/manual/en/function.empty.php
Edit -- https://master.php.net/note/edit/78596
Del: integrated -- https://master.php.net/note/delete/78596/integrated
Del: useless -- https://master.php.net/note/delete/78596/useless
Del: bad code -- https://master.php.net/note/delete/78596/bad+code
Del: spam -- https://master.php.net/note/delete/78596/spam
Del: non-english -- https://master.php.net/note/delete/78596/non-english
Del: in docs -- https://master.php.net/note/delete/78596/in+docs
Del: other reasons-- https://master.php.net/note/delete/78596
Reject -- https://master.php.net/note/reject/78596
Search -- https://master.php.net/manage/user-notes.php