Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128569 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 lists.php.net (Postfix) with ESMTPS id 2A5AF1A00BC for ; Wed, 27 Aug 2025 07:50:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756280945; bh=JYWTSL5UjLkmEEqKzxWoLnRrg900H0QpG3Wmj/YExmU=; h=Date:From:To:In-Reply-To:References:Subject:From; b=mdPfFw0opBv2mehe1JCh4GtjyJKiqQOWYXvjQNBUj19/EkgoExxRZuDsHURdbpq97 1yCGYClx7agVw/DnuHNi6M/vUJVj5g4ChxrbkS87XHIi0zT2iFzUR87J1bBnsX57e/ SqGDJOOb/itqhA4eEzTG0aZCAgESBLhms6ClXMjwo+DKfsy5TNw7hGYZSgb60I50Tf vMYb2g8UmGSpTKzILgM/wiXHiRvWj64BUqd/ZNsVIVKmbmYWqg+Wjazk5c+gbVL/rq sZqwaGErC2oPQ7gAqtW9an/SP5rx9Fnj/aN8sC8MaHat9KS3QFT6GvDqpLS/e+e2XX ExXgy+ECyCF2g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6884D180081 for ; Wed, 27 Aug 2025 07:49:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) 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.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a3-smtp.messagingengine.com (fhigh-a3-smtp.messagingengine.com [103.168.172.154]) (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 ; Wed, 27 Aug 2025 07:49:03 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id 607751400109 for ; Wed, 27 Aug 2025 03:50:35 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Wed, 27 Aug 2025 03:50:35 -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=fm2; t=1756281035; x=1756367435; bh=zWoTT18QXz uFqslAM2ITCTf9xFUlEVi9saMiZPx6rHw=; b=hAKgcMfAIp0hl1QTLwz1hXfjLU S5PLTQzOR7EbDeubH5gMdlwDuz5co8WhWlHO0oLffEAuCF/afjxBhnyGqQk6wRz6 3LGUqI9vbIkljgdp+xKphQWPlqg8mFJYXAu104CTJQWDAEWIV8TclkK2FMrUKyEd E+LDi6Fxq/aP1LB6kQo8kqs4uJSIMdtvQ89DTzm6VHaz3c/RNvLbYNUwTQpeP7St bA6Sk75PIWZbwZ3BMuYJPcmcgd27qnfwHTjPhfQUlFXRfZHrEgVHtdrXzZyBMGVe zQtiIOkRPl2ceKOvR0XN8nl87gtMrreimE/47h3gv3i8S4a4gaTiADjFelVg== 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=fm1; t= 1756281035; x=1756367435; bh=zWoTT18QXzuFqslAM2ITCTf9xFUlEVi9saM iZPx6rHw=; b=khy0xjZybYB5sJDXhcEQ9KOrc7NkwSf7JfWhAgvPMw/4LW6cYvW zCXbEkAPlCrXDnZJO2NJSf86DQYp/V/3TC/fYA6Uesfs2XDKYiKdW139+y0KVAw7 TyLrEc/9HS3/FZYf56DsabnXM0V2mfjCVfQeAr4DNzph6bJP1P+OiH+2wbfl5KkL nNkx5CsRMgvpKeOnEdfEzmFxrSXZBUyBj3zcAKPgcHZDRDj0WLAS2CZ4N54qXhT4 BIisGG3LJ2uj+J4tZur4xw7tK6H3u01n5cZxsVmr263LAKI8qiqwXk6144IOHMQN WxNYR33DLdv9+aoBFC5RKEkqEy3O7BPQz1A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgddujeejiedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefoggffhffvkfgjfhfutgesrgdtreerre dtjeenucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggu rdgtohguvghsqeenucggtffrrghtthgvrhhnpeeitdfhhfdvfffhtedtgfevfefgueegge duueekjeehieeggffhieevleeffeeufeenucffohhmrghinhepghhithhhuhgsrdgtohhm necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosg essghothhtlhgvugdrtghouggvshdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhht phhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id EF40B1820074; Wed, 27 Aug 2025 03:50:34 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: AJ2Gy4vr0DuR Date: Wed, 27 Aug 2025 09:50:14 +0200 To: internals@lists.php.net Message-ID: In-Reply-To: References: <642a1575-a855-442a-aa3c-3fa123e793d7@bastelstu.be> Subject: Re: [PHP-DEV] [RFC] Add "is_representable_as_float()" and "is_representable_as_int()" functions Content-Type: multipart/alternative; boundary=ff80a879b7dc431284a55a6214fce322 From: rob@bottled.codes ("Rob Landers") --ff80a879b7dc431284a55a6214fce322 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Aug 26, 2025, at 21:36, Larry Garfield wrote: > On Tue, Aug 26, 2025, at 1:36 PM, Tim D=C3=BCsterhus wrote: > > Hi > > > > On 8/26/25 16:48, Larry Garfield wrote: > >> I have multiple times just recently had need of "I have a numeric s= tring, should I cast it to an int or a float?", for which an is_represen= table_as_int() function (or similar) would be quite helpful, and neater = than the messy solution I usually use. > > > > It would've been nice to know what that use-case is, rather than jus= t=20 > > knowing that you had that use-case. > > > > I'm having a hard time thinking of something where I don't a-priori = know=20 > > what type I expect to get and would need to inspect the value to mak= e a=20 > > decision. > > > > I see how having a function that safely coerces a string into an int= ,=20 > > returning null if coercion fails, basically intval() with better err= or=20 > > handling and taking only `string`s, could be useful, but that's not = what=20 > > is being asked here. > > > > Best regards > > Tim D=C3=BCsterhus >=20 > When doing generic serialization, the input is often always-strings (e= g, environment variables, HTTP Query parameters, etc.) When doing gener= ic code (not type generics, but "works on anything" kind of generic), I = often have to resort to this: >=20 > https://github.com/Crell/EnvMapper/blob/master/src/EnvMapper.php#L88 >=20 > Which gets the job done, but feels ugly to me. >=20 > Even if I know what the target type is, I still need to ask the questi= on "so does this string match the target type?" =20 >=20 > https://github.com/Crell/Carica/blob/master/src/Middleware/NormalizeAr= gumentTypesMiddleware.php#L73 >=20 > "I have a string, the parameter wants an int, is that even possible?" = Being able to replace that floor() nonsense with is_integerable() (by w= hatever name) would make my life a lot easier. >=20 > For float, is_numeric() is already sufficient for my purposes. I just= need to be able to differentiate between "3" and "3.14" to cast to the = correct type. >=20 > --Larry Garfield >=20 Isn't this the entire use-case of type coercion? =E2=80=94 Rob --ff80a879b7dc431284a55a6214fce322 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Tue, Aug = 26, 2025, at 21:36, Larry Garfield wrote:
On Tue, Aug 26, 2025, at 1:36 PM, Tim D=C3=BC= sterhus wrote:
> Hi
>
> On 8/26/2= 5 16:48, Larry Garfield wrote:
>> I have multiple times = just recently had need of "I have a numeric string, should I cast it to = an int or a float?", for which an is_representable_as_int() function (or= similar) would be quite helpful, and neater than the messy solution I u= sually use.
>
> It would've been nice to know = what that use-case is, rather than just 
> knowing tha= t you had that use-case.
>
> I'm having a hard= time thinking of something where I don't a-priori know 
= > what type I expect to get and would need to inspect the value to ma= ke a 
> decision.
>
> I see = how having a function that safely coerces a string into an int, 
> returning null if coercion fails, basically intval() with b= etter error 
> handling and taking only `string`s, cou= ld be useful, but that's not what 
> is being asked he= re.
>
> Best regards
> Tim D=C3=BC= sterhus

When doing generic serialization, the i= nput is often always-strings (eg, environment variables, HTTP Query para= meters, etc.)  When doing generic code (not type generics, but "wor= ks on anything" kind of generic), I often have to resort to this:
<= div>

Which gets the job= done, but feels ugly to me.

Even if I know wha= t the target type is, I still need to ask the question "so does this str= ing match the target type?"  


"I have a string, the parameter wants an int, is that = even possible?"  Being able to replace that floor() nonsense with i= s_integerable() (by whatever name) would make my life a lot easier.

For float, is_numeric() is already sufficient for m= y purposes.  I just need to be able to differentiate between "3" an= d "3.14" to cast to the correct type.

--Larry G= arfield


Isn't this = the entire use-case of type coercion?

=E2=80=94 Rob
--ff80a879b7dc431284a55a6214fce322--