note 53149 deleted from function.empty by didou
| From: | didou@php.net | Date: | Sun, 18 May 2008 00:59:47 +0000 |
| Subject: | note 53149 deleted from function.empty by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139488@lists.php.net to get a copy of this message | ||
Note Submitter: admin at ninthcircuit dot info
----
Something to note when using empty():
empty() does not see a string variable with nothing but spaces in it as "empty" per se.
Why is this relevant in a PHP application? The answer is.. if you intend to use empty() as a means
of input validation, then a little extra work is necessary to make sure that empty() evaluates input
with a more favorable outcome.
Example:
<?php
$spaces = " ";
/* This will return false! */
if (empty($spaces))
print "This will never be true!";
else
print "Told you!";
?>
To make empty() behave the way you would expect it to, use str_replace().
<?php
$spaces = str_replace(" ",""," ");
/* This will return true! */
if (empty($spaces))
print "This will always be true!";
else
print "Told you!";
?>
This might seem trivial given the examples shown above; however, if one were to be storing this
information in a mySQL database (or your preferred DB of choice), it might prove to be problematic
for retrieval of it later on.