Re: RFC: blank() Function as a Complement to empty()
| From: | Rob Landers | Date: | Sat, 05 Apr 2025 20:00:15 +0000 |
| Subject: | Re: RFC: blank() Function as a Complement to empty() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127057@lists.php.net to get a copy of this message | ||
On Sat, Apr 5, 2025, at 14:15, Kayck Matias wrote:
> *INTRODUCTION*
> This RFC proposes the addition of a new global function
blank()
> to PHP’s core, intended to provide a more intuitive and context-sensitive way of checking for
> "blank" values. This function is *not a replacement* for
> empty(), but a *complementary tool *to help developers more
> accurately evaluate user input, query parameters, and form values — particularly in situations
> where empty() may behave in unintuitive ways.
>
>
> *MOTIVATION
*The motivation for this RFC arose from a real-world issue I encountered at work involving query
string filtering — specifically when using the string "0" as a filter
value. Because empty("0") evaluates to true, the logic skipped
this valid input, causing incorrect behavior in the application. This highlighted a gap in PHP’s
current toolset for handling “blank” values in a more semantic and intention-aligned way.
I would personally advocate that empty() NEVER be used on strings. There are too many edge cases. As
I said in an earlier message on the thread, if you want to know if a string is empty, just do
$string == "" -- note the double equals, not triple equals. This matches null, an actual
empty string, and false, but won't match anything else.
> *PROPOSAL*
> The proposed blank() function will behave similarly to
> empty(), but with semantics better suited for filtering, validation,
> and user input.. Its primary goals are:
> • To treat whitespace-only strings as blank (e.g., "
> ").
>
> • To treat "0" (string zero) and
> 0 (int zero) as *not blank*, unlike ÎUo¬g|˜
> "XO›’ֳempty().
>
> • To support clearer, intention-driven logic when working with dynamic input, especially in
> query strings and HTTP forms.
>
> Function Signature
>
> function blank(mixed $value): bool;
> **
> Logic (PHP version)
> function blank(mixed $value): bool
> {
> if (
> false === $value ||
> (empty($value) && '0' != $value) ||
> (\is_string($value) && '' === \trim($value))
> ) {
> return true;
> }
>
> return false;
> }
> Examples
> echo blank(null); // true
> echo blank(false); // true
> echo blank(""); // true
> echo blank(" "); // true
> echo blank([]); // true
>
> echo blank(0); // false
> echo blank("0"); // false
> echo blank("test"); // false
> echo blank([0]); // false
I agree with most of these. I do not agree that " " (a space) is blank though. For people
without last names, this is often their last name to pass validation on forms. As I said earlier, a
simple loose check with an empty string is usually all you need, not empty().
> Best Regards,
> Kayck Matias ☕
— Rob