Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127058 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 B78BF1A00BC for ; Sat, 5 Apr 2025 20:23:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743884489; bh=P3t1xgw037iNk1vyE4mp7RnLTJlOWzzr9SDdr6PPI+A=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=X2A602O1uZj9g6skh1xdlophMzVmOtqFMx4VihHPCfrDdLMYsFH+cb5RrJuSB6Rgv fu4Nw+mp1T3Ki/9YE0dPBd6hNOW015ohDNVgEiUnQECVFiyzLks7I5aERYEjxQwWDG CGMda9hulMksQaT865UOww472/bfx0nL3anaIlIb7KDxZdbNgtTgdlweXBEdkFhYzw HZqe66KfQgJrQ+tywHn8THcbKcHYy9F2mC3GnwXdOUtOZBWQaS8MzSmNtJEbZS/uRE zggXuR23s6aKIqTkmmqrKcyzC3dXJd0HOlOgiUqeRq6nUY8Ay7GDVRFPMonddtCvJW Vvv/rJTFv2B+w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1E54C18005B for ; Sat, 5 Apr 2025 20:21:28 +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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 5 Apr 2025 20:21:27 +0000 (UTC) Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-abf3d64849dso481576066b.3 for ; Sat, 05 Apr 2025 13:23:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743884632; x=1744489432; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=8n+A8tyS6KcKMzUf4N7mi99IeaaEftd+YpxueHCB0RE=; b=M32eaAsutctLUJE/qD6vm0L/lvonjt/FFa0EfEOF9wnb68F+x9c7xLgisC2LvnSuL4 k93pYZ1ps39/xnQQjnABZ4VAD82uUl1v+AIOtOHpLT7Xtl1zBTGNj7kODEa8Mp4ewKwE SjKIX7JzrQlml/4++pyxllmqNYtF810dkEA613xqAR0bKsFE+fVRvTpzX6Oo3LMrg6zN pbPbD2tNUealSOD0HZoaJFO0LZ0WBGbMvE//NU5ebKr1rz2JXlRBXYy+L10Af4snBdPO l6gEVlcBCHnVc+GbYiguONJAFWj4BAblS9FHLMnl1rn6dXHZiD1ENW+qrw9XB+VziCjW IQXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743884632; x=1744489432; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8n+A8tyS6KcKMzUf4N7mi99IeaaEftd+YpxueHCB0RE=; b=TFQNvK2WIRXA9jJ0esIphw2o6qkuEBhNXZURy/uafEy+cUhvm4X1whLPuUQZnNQ9QE akLSWE04IhAP7FpnRAxdutXASmIGI3FPnyuAbvgFn4KZ6JKK0hAK2eyMQcu55eSgasU4 aPn4WzwtUT6+CeJE5WjcFs1JeHk2nZDMG8tZUq518kGnaLnHfxRMSoNF7hd+bMucXijK 6nvC/BVNHZb+SpQOgqDJqZcHFKEyrZ8ajch6pmMKfLZCsfkQHz+NNJX32u84pEb+vk+h gWCmFPFLjCVarx91j+R0GIwy6k/6mo1Kh84qcPWG1VEHSfGpWsF548G8ZTIyKDlsa8xz KRDQ== X-Gm-Message-State: AOJu0YzvVcWfa1/o3uQI0Le4TbYCsZOU8/dMBAaYJxcldgI0sMzeCGKN USWKXBX+F8kqqRmP/R0DPKxmWSMxQxh5PmmneOSls1ePZ5xknTtqn3v9KLEZgUw4jDGnLkSQLNz p1Ck8RlINn//bzI+JkGMEJqiSMLE= X-Gm-Gg: ASbGncsNnX5ctRgSxWRb1zt4MitGjDohO3Ctncd2c4gO/KmVy32wac+TLWSHhXiE7xg yg8WOR4fDJS8ya4pnGIuCwMmsIbAIkD6X592PA0FYf18FLHndsnNSK0KEbFScnsEjdifwHcBw2r Ld0iffAcTJ0MEOUw1LJSbeFi68Qw== X-Google-Smtp-Source: AGHT+IH3ysl0xQZV/PakKNyr24P2PRSa0VzVLYbWHvEnCJOTvHXxw+NE2xsI4LLTU/7yvAnk7M+k6CPOxybQKd9778Q= X-Received: by 2002:a17:907:6e90:b0:ac3:121e:f2cb with SMTP id a640c23a62f3a-ac7d16bc073mr673147866b.1.1743884631576; Sat, 05 Apr 2025 13:23:51 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <17aa0250-ee1f-477a-a255-303281d7a833@app.fastmail.com> In-Reply-To: <17aa0250-ee1f-477a-a255-303281d7a833@app.fastmail.com> Date: Sat, 5 Apr 2025 23:23:40 +0300 X-Gm-Features: ATxdqUHHMKcIjg7ZBUBPSMayhaexP_dxMBMVLBZodjBN5_lJ-9qfk94wwA3WyHk Message-ID: Subject: Re: [PHP-DEV] RFC: blank() Function as a Complement to empty() To: Rob Landers Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000fe985906320dc76f" From: i.miroslavov@gmail.com (Iliya Miroslavov Iliev) --000000000000fe985906320dc76f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Apr 5, 2025 at 11:04=E2=80=AFPM Rob Landers wro= te: > 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=E2= =80=99s > 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 =E2=80=94 particul= arly 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 =E2=80=94 specifical= ly 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=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 used on strings. There > are too many edge cases. As I said in an earlier message on the thread, i= f > 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(), 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 > 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 =3D=3D=3D $value || > (empty($value) && '0' !=3D $value) || > (\is_string($value) && '' =3D=3D=3D \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 =E2=98=95 > > > =E2=80=94 Rob > Just something that came into my mind. A "normal space" character is 32, "non-breaking space" is 160 and a "zero width space" is 8203. These are all characters so they are not blank and cannot be ignored --=20 Iliya Miroslavov Iliev i.miroslavov@gmail.com --000000000000fe985906320dc76f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Sat, Apr 5, = 2025 at 11:04=E2=80=AFPM Rob Landers <rob@bottled.codes> wrote:
On Sat, Apr 5, 2025, at 14:15, Kayck Mat= ias wrote:
INTRODUCTION
This RFC proposes the addition of a new global function=C2=A0blank()= =C2=A0to PHP=E2=80=99s core, intended to provide a more intuitive an= d context-sensitive way of checking for "blank" values. This func= tion is=C2=A0not a replacement=C2=A0for=C2=A0empty(), b= ut a=C2=A0complementary tool=C2=A0to help developers more accurately= evaluate user input, query parameters, and form values =E2=80=94 particula= rly in situations where=C2=A0empty()=C2=A0may behave in unintu= itive ways.


MOTIVATION
The motivation for this RFC arose from a real-world issue I encountered a= t work involving query string filtering =E2=80=94 specifically when using t= he string=C2=A0"0"=C2=A0as a filter value. Because= =C2=A0empty("0")=C2=A0evaluates to=C2=A0true, the logic skipped this valid input, causing incorrect behavior in th= e 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-al= igned way.

I would person= ally advocate that empty() NEVER be used on strings. There are too many edg= e 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 doub= le equals, not triple equals. This matches null, an actual empty string, an= d false, but won't match anything else.

PROPOSAL
The proposed=C2=A0blank(= )=C2=A0function will behave similarly to=C2=A0empty(), = but with semantics better suited for filtering, validation, and user input.= Its primary goals are:
  • To treat whitespace-only strings as blank (e.g.,=C2=A0" "<= /code>).

  • To treat=C2=A0= "0"=C2=A0(string zero) and=C2=A00=C2=A0(int z= ero) as=C2=A0not blank, unlike=C2=A0empty().

  • To support clearer, intention-driven lo= gic when working with dynamic input, especially in query strings and HTTP f= orms.

Function Signatur= e

function blank(mixed $value= ): bool;

Logic (PHP vers= ion)
function = blank(mixed $value): bool
{
= if (
= false =3D=3D=3D $va= lue ||
<= span style=3D"font-size:10px"> (empty($value) && '0' !=3D $value) ||
(\is_string<= /span>($value) && '' =3D=3D=3D \= trim($value))
= ) {
return true;
= }

return false;=
}
Examples
echo<= /span>=C2=A0blank(null);=C2=A0// true
echo=C2=A0blank(false);=C2=A0// true
echo=C2=A0blank("");=C2=A0// true
echo=C2=A0blank("=C2=A0");=C2=A0//= true
echo=C2=A0blank([]);=C2=A0// true

echo=C2=A0blank(0);=C2=A0// false
echo=C2=A0blank("0");=C2=A0// false
echo=C2=A0blank(<= span style=3D"color:rgb(233,242,132)">"test");=C2=A0// false
<= div>echo=C2=A0blank([0]);=C2=A0// 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 a= n empty string is usually all you need, not empty().

Best Regards,
Kayck Matias=C2=A0=E2=98=95

=E2=80=94 Rob

Just something that came into my mind. A "normal spac= e"=C2=A0character is 32, "non-breaking space" is 160 and a &= quot;zero width space" is=C2=A08203. These are all characters so they = are not blank and cannot be ignored
--
Iliya Miroslavov Iliev
--000000000000fe985906320dc76f--