Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128568 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 C07681A00BC for ; Tue, 26 Aug 2025 20:39:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756240661; bh=TZ+Is8jouAB0fus5ZK6cxYnSf+cTe5J37OnmB3FaCs0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=V8PsrgYl+8IJvj7uVP0qjcRCbQXFlrBVKq4Vr95eplQi2WUnb+RJ2mwklUZVtxHCO g+4K9ROxNQpBHQT5bN0cvuLmxuZoqXT/LBqOvJ4wXNrrUrvdNVyd0tKb3Gox4CFaMb Ys3AIzS11q4x8TmFkSfU52vp5V3tlxxHrCwO4eZ9wWVbUsMvt8XMH0nSyZudUBn2yK 2eNFua0isacqU4mkDuPItiod4BaoxGqqbK2WDk7Zf/N0aeFFahrmTE40t6BsBw2G/9 evBZGTjvD+iZIelTEhkbULVwv3GG14kh60/IdnnfMqwlw9CowchU2DZmn12inWCp7X zvYjRdi8vq8lQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 309AA1801D7 for ; Tue, 26 Aug 2025 20:37:40 +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.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_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (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 ; Tue, 26 Aug 2025 20:37:39 +0000 (UTC) Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-7e8706491ecso867492085a.2 for ; Tue, 26 Aug 2025 13:39:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756240751; x=1756845551; 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=xloFDOEOFo3JF1C/hY6tppjzNbOwQo7gvvgNRYRlWaY=; b=NBSPZaaLm+6VMLAVHY5XH98jnznSPX+pPH50HVzG6kxC721taaD7hI8q3g0skQOfD0 sT6taNhXVlfl88HTnjbBXDVwQOXrCg5llNRgoF6WjeUwHvsbWdt4ef4PP3Pe+6hBacWL l5Vbgk1yDyr7pk7LsRL1Qsq6LL3/gUuFb/eWXQqw3fdtk/8f953eLQPiD6BkhIfk8iMm tqScf+kwNDjxYIoXPyo56Mpba/CtsUodAhAigMiyCVucIzWicZi6ankP5OeSFpyx+U6Q pHygM76/XyeWjlrkuloHKBJJrsgsL2iCpzWY4qvuJ320OOHrtt0Z55JIbOAuVqbS7PSb Oi1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756240751; x=1756845551; 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=xloFDOEOFo3JF1C/hY6tppjzNbOwQo7gvvgNRYRlWaY=; b=dIXXgGW0dMtbckciQiG9RQfX06+fwMN2EPQv2OFwUS5qj43ZWW2iga709jiiC4ToLl O/teFZIBhEuaK7rD3iwPK63UXbyT/UeEKXWEFnS+Aa6VZ5jyDslS+Osdz8MVffkLEKo4 X4HO6C7WRbZQwASDxdxOgrq1ZA5N830hWLbPJqa28jvY4KMKwH/mxCDRDzKHnwDWtojf 8atyAFG9/4iSb9crf7STzzhHyx1vEEyt/KgdwJH+nCkzDQtvSQNAptcYZtvgKWZ4DkvZ La8hRP66Zzn5I2l8Otwc7tjhRKXY/MOG6oYHlxiGpEWjaee4ppriX8yW6Y0zyYaPABZZ gQcw== X-Gm-Message-State: AOJu0YzXxcKL0QVnMVZ+ocJDlHDVSjg0VaP/t4UhbhN/vCRIJJ40okUm 555rB7APJSyBJtbEFnQ1Eo4nwdxqym27dEEK2Ixutw/c/K3tOrHORxLiMyXpZXLYZ+1z512Hmn0 J5z521ygATmTLDS2LTGSqGN0ABcdv25s= X-Gm-Gg: ASbGnct1wHpLFVZEY2om6XPoykxpa9mSGDguk1kNIJxV/Xjy3d7x23aVlQL7VDolwta T0jDSYiuh7Ih3aj9Cr9JaF9Uy+Emqa3xVfCK572A0Qc8n1a6fp6X1OreprfoUp1b5oALQfbdvCd KzKoUODDfRfUQB7j76ZO5wggqD5JIImyPd1peKHAGqWEC7/8dVWVR2mOK+hSPh1lJCPtwW2R9N2 eY7Cfex+kmo665PKA99IHCGM4vTd+r18i2Pp4gd2Q3sCThpJUqC X-Google-Smtp-Source: AGHT+IHXwv7K1jEoqBOuZVBMXlKYbOidykEWmzp01qK801f3HphXx98oZ7DrhiYbX3l//hwVS24OH6btev+OSKaegQ0= X-Received: by 2002:a05:620a:3720:b0:7e7:fdd2:cc58 with SMTP id af79cd13be357-7ea10f94e8bmr1996566785a.15.1756240751364; Tue, 26 Aug 2025 13:39:11 -0700 (PDT) Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <642a1575-a855-442a-aa3c-3fa123e793d7@bastelstu.be> In-Reply-To: Date: Tue, 26 Aug 2025 22:38:59 +0200 X-Gm-Features: Ac12FXwuIN1RXHf5JE69GZfQWRHsnbQXv3GTAp5jcAsTZ1lPLL9A3OaJJFTTOgg Message-ID: Subject: Re: [PHP-DEV] [RFC] Add "is_representable_as_float()" and "is_representable_as_int()" functions To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000201003063d4aaa14" From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000201003063d4aaa14 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Le mar. 26 ao=C3=BBt 2025 =C3=A0 21:38, Larry Garfield a =C3=A9crit : > 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 > 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, a= nd > neater than the messy solution I usually use. > > > > It would've been nice to know what that use-case is, rather than just > > knowing that you had that use-case. > > > > I'm having a hard time thinking of something where I don't a-priori kno= w > > what type I expect to get and would need to inspect the value to make a > > decision. > > > > I see how having a function that safely coerces a string into an int, > > returning null if coercion fails, basically intval() with better error > > handling and taking only `string`s, could be useful, but that's not wha= t > > is being asked here. > > > > Best regards > > Tim D=C3=BCsterhus > > When doing generic serialization, the input is often always-strings (eg, > environment variables, HTTP Query parameters, etc.) When doing generic > code (not type generics, but "works on anything" kind of generic), I ofte= n > have to resort to this: > > https://github.com/Crell/EnvMapper/blob/master/src/EnvMapper.php#L88 > > Which gets the job done, but feels ugly to me. > > Even if I know what the target type is, I still need to ask the question > "so does this string match the target type?" > > > https://github.com/Crell/Carica/blob/master/src/Middleware/NormalizeArgum= entTypesMiddleware.php#L73 > > "I have a string, the parameter wants an int, is that even possible?" > Being able to replace that floor() nonsense with is_integerable() (by > whatever name) would make my life a lot easier. > > 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. > > --Larry Garfield > Why not just 0 + $theValue? Then the engine will decide. No? > --000000000000201003063d4aaa14 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


Le mar. 26 ao=C3=BBt 2025 =C3=A0= 21:38, Larry Garfield <larry@= garfieldtech.com> a =C3=A9crit=C2=A0:
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 num= eric 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 ne= ater than the messy solution I usually use.
>
> It would've been nice to know what that use-case is, rather than j= ust
> knowing that you had that use-case.
>
> I'm having a hard time thinking of something where I don't a-p= riori know
> what type I expect to get and would need to inspect the value to make = a
> decision.
>
> I see how having a function that safely coerces a string into an int, =
> returning null if coercion fails, basically intval() with better error=
> handling and taking only `string`s, could be useful, but that's no= t what
> is being asked here.
>
> Best regards
> Tim D=C3=BCsterhus

When doing generic serialization, the input is often always-strings (eg, en= vironment variables, HTTP Query parameters, etc.)=C2=A0 When doing generic = code (not type generics, but "works on anything" kind of generic)= , I often have to resort to this:

https://github.com/Cr= ell/EnvMapper/blob/master/src/EnvMapper.php#L88

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

Even if I know what the target type is, I still need to ask the question &q= uot;so does this string match the target type?"=C2=A0

https://github.com/Crell/Carica/blob/master/src/Middleware/Norm= alizeArgumentTypesMiddleware.php#L73

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

For float, is_numeric() is already sufficient for my purposes.=C2=A0 I just= need to be able to differentiate between "3" and "3.14"= ; to cast to the correct type.

--Larry Garfield


Why not just 0 + $theValue? The= n the engine will decide. No?
--000000000000201003063d4aaa14--