Reputation: 8086
I've been using the empty
function to validate if a form submission is made blank. However, I just realized that empty
passes " *whitespace* "
as NOT empty, which is obviously not what I want.
Examples:
$text = "is this empty?";
if (empty($text)) {
echo 'Yes, Empty!';
}
// return is NOT empty
$text = " ";
if (empty($text)) {
echo 'Yes, Empty!';
}
// return is NOT empty
Right now I am using:
$text = " ";
if (!trim($text)) {
echo 'Yes, Empty!';
}
// return is Yes, Empty!
I need to understand:
empty
used for?trim
for my purposes?Upvotes: 2
Views: 174
Reputation: 1962
Upvotes: 1
Reputation: 219824
Use both trim()
and empty()
together:
$text = " ";
if (empty(trim($text))) {
echo 'Yes, Empty!';
}
Upvotes: 3