Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127057 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id D4C161A00BC for ; Sat, 5 Apr 2025 20:00:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743883093; bh=HAil169CAU4ugBS6M1u89G97B4KfVPLCTL6UQMJ0RYU=; h=Date:From:To:In-Reply-To:References:Subject:From; b=lCHc5/cSZyoyKPWpW0zQDZrTqUU+I4V42L5XZEjMk1MZwnl3L3DTIjZFqQoWS4aA3 6Is0rF+z6cl3EsksHgCgPbcFCttd6YWWjyyKGmnA1BSd2AYQxRlirkkdkkHZaG1N6E StghFXL/dJILtdBKuvpEoXmgWCdU0qWmrxuMiS3+sxcQfz8K5YNlzTeKT2KZgq/BNu LkgFW+OUV86NEyjpayCk4OLeNMdiiEOD0tu2ok+HBFtWbt53bsddj0dGF/JaAIEfya nUXsUIwfWSXrJM+XpukXnU11OhHMdALk+Mei0e6BenkMwzVdurZXwKLe9Y/hSRsmwd 7FDIA+YxrdFcQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6812F180056 for ; Sat, 5 Apr 2025 19:58:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-b6-smtp.messagingengine.com (fout-b6-smtp.messagingengine.com [202.12.124.149]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 5 Apr 2025 19:58:12 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.stl.internal (Postfix) with ESMTP id 6F1C31140100 for ; Sat, 5 Apr 2025 16:00:37 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-12.internal (MEProxy); Sat, 05 Apr 2025 16:00:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1743883237; x=1743969637; bh=NTewEwu1fA KD2LyHbSvri+1efdO+DUmz/0kIsC+PI7Q=; b=ucSn7k3xaJlzUrMvEQhbyE0A8P CMRq7lWsBwDty+dKA4DRbY26cegweL7QdWx26r1rMobPfNthof1yogGcaLZ4n2Rs bqdPY9kfoDeWvtTUJYwdWUDqKGeTQSrQwol/BvR9XcKRuLjOXS69uBcuJZr+3Xrt 0WHPGSs3CH658rU2pOzSClY5G8LeTZnsbtv7MCb9ODbQwOJlSscn8xfk7VZrDC7S EW06REp80rwXfZAwVh7L5umGiPdZgA94WKSk8NUtyUbBFiobtD63NcNLMf4Ft8ev DBOt7MmHzJMCgYhCd6/+W+LIjPdpMPIQQmEWK4KxMiTHnUN92B7XKu9GnhcA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1743883237; x=1743969637; bh=NTewEwu1fAKD2LyHbSvri+1efdO+DUmz/0k IsC+PI7Q=; b=SV8La8cxxYbxO3JJKSB5cxeZWSJKlz4AO7Bjr6xzd/w+6rLZHEU An1vsb7a2X1VrxCPnriM7+bnWxIKOTNQ4bwv2HTuJ6rz5xwiz9q156kyFnuc7F1I tq0jbOtY1W98XvK0joTM5CBKKHPyXMQpf/izQg47iVMGQLvLd1b+dhVptI9pq2+M KcvthecRXGMPlKeII4EuEWISHazFXJx2bqn1RpzUedqKQQTgjxKGjDxejgn2KD5U 094P33sp66wScDTODfd6ZjghZcwYAQ9uMbrq82OZAfvkrDBt/IU4GmNkMiUyA9DU So3zR4VqutMYWMi7xUqfTRIDXQewEfykzoA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduleehvdeiucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefogg ffhffvkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfnrghnuggvrhhs fdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtthgvrhhnpedtue ejtdethfeulefhtdelieduteelffdtudelheffgedtieehhfelieejgfevgeenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgssegsohhtth hlvggurdgtohguvghspdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdp rhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id E9846780069; Sat, 5 Apr 2025 16:00:36 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: Td40fe37c6836e36c Date: Sat, 05 Apr 2025 22:00:15 +0200 To: internals@lists.php.net Message-ID: <17aa0250-ee1f-477a-a255-303281d7a833@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] RFC: blank() Function as a Complement to empty() Content-Type: multipart/alternative; boundary=c7c4e5d18cbd4b84b42b519034ee24a6 From: rob@bottled.codes ("Rob Landers") --c7c4e5d18cbd4b84b42b519034ee24a6 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sat, Apr 5, 2025, at 14:15, Kayck Matias wrote: > *INTRODUCTION* > This RFC proposes the addition of a new global function `blank()` to P= HP=E2=80=99s core, intended to provide a more intuitive and context-sens= itive way of checking for "blank" values. This function is *not a replac= ement* for `empty()`, but a *complementary tool *to help developers more= accurately evaluate user input, query parameters, and form values =E2=80= =94 particularly in situations where `empty()` may behave in unintuitive= ways. >=20 >=20 > *MOTIVATION *The motivation for this RFC arose from a real-world issue I encountered= at work involving query string filtering =E2=80=94 specifically when us= ing the string `"0"` as a filter value. Because `empty("0")` evaluates t= o `true`, the logic skipped this valid input, causing incorrect behavior= in the application. This highlighted a gap in PHP=E2=80=99s current too= lset for handling =E2=80=9Cblank=E2=80=9D 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 =3D=3D "" -- = 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()`, bu= t with semantics better suited for filtering, validation, and user input= . Its primary goals are: > =E2=80=A2 To treat whitespace-only strings as blank (e.g., `" "`). >=20 > =E2=80=A2 To treat `"0"` (string zero) and `0` (int zero) as *not bla= nk*, unlike `empty()`. >=20 > =E2=80=A2 To support clearer, intention-driven logic when working wit= h dynamic input, especially in query strings and HTTP forms. >=20 > Function Signature >=20 > function blank(mixed $value): bool; > ** > Logic (PHP version) > function blank(mixed $value): bool > { > if ( > false =3D=3D=3D $value || > (empty($value) && '0' !=3D $value) || > (\is_string($value) && '' =3D=3D=3D \trim($value)) > ) { > return true; > } >=20 > return false; > } > Examples > echo blank(null); // true > echo blank(false); // true > echo blank(""); // true > echo blank(" "); // true > echo blank([]); // true >=20 > 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 t= hough. For people without last names, this is often their last name to p= ass 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 =E2=98=95 =E2=80=94 Rob --c7c4e5d18cbd4b84b42b519034ee24a6 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Sat, Apr 5, = 2025, at 14:15, Kayck Matias wrote:
INTRODUCTI= ON
This RFC proposes the addition of a new global func= tion blank() to PHP=E2=80=99s core, intended to p= rovide a more intuitive and context-sensitive way of checking for "blank= " values. This function is not a replacement for <= code>empty(), but a complementary tool to help d= evelopers more accurately evaluate user input, query parameters, and for= m values =E2=80=94 particularly in situations where empty() may behave in unintuitive ways.

=
MOTIVATION
The motivation for this RFC arose fr= om a real-world issue I encountered at work involving query string filte= ring =E2=80=94 specifically when using the string "0"&= nbsp;as a filter value. Because empty("0") evalua= tes to true, the logic skipped this valid input, causi= ng incorrect behavior in the application. This highlighted a gap in PHP=E2= =80=99s current toolset for handling =E2=80=9Cblank=E2=80=9D values in a= more semantic and intention-aligned way.
=

I would personally advocate that empty() NEVER be us= ed on strings. There are too many edge cases. As I said in an earlier me= ssage on the thread, if you want to know if a string is empty, just do $= string =3D=3D "" -- note the double equals, not triple equals. This matc= hes null, an actual empty string, and false, but won't match anything el= se.

PROPOSAL
= The proposed blank() function will behave similar= ly to empty(), but with semantics better suited for fi= ltering, validation, and user input. Its primary goals are:
  • To treat whitespace-only string= s as blank (e.g., " ").

  • To treat "0" (string zero) and&n= bsp;0 (int zero) as not blank, unlike = ;empty().

  • To= support clearer, intention-driven logic when working with dynamic input= , especially in query strings and HTTP forms.

Function Signature
<= /h3>
function blank(mixed $value): bool;

Logic (PHP version)
function blank(mixed<= /span> $value): bool
{
if (=
= false =3D=3D=3D $value ||
(empty($value) &a= mp;& '0' !=3D $value) ||<= br>
(\<= span style=3D"color:rgb(139, 233, 253);">is_string($value) && '' =3D=3D=3D \trim($value))
) {
return true;
}
=

return false;
}

Examples
ech= o blank(null); // true
echo blank(false<= /span>); // true
echo <= span style=3D"color:rgb(80, 250, 123);">blank(""); // true
echo blank<= /span>(" "); // tr= ue
echo blank([]);&nb= sp;// true

echo&nb= sp;blank(0); // false
echo bl= ank("0"); // fal= se
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 =E2=98=95

=E2=80=94 Rob
= --c7c4e5d18cbd4b84b42b519034ee24a6--