Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128567 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 65C8B1A00BC for ; Tue, 26 Aug 2025 19:36:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756236895; bh=Nso1aZYhWMz/wr/VVx5qD0/GdKhaQq562oQCFvCrRqM=; h=Date:From:To:In-Reply-To:References:Subject:From; b=bfmZK+Clvqae7ALN6oEJ/UnpknDBfq4ge58dIGGkObTN7IF4TSCCuQIMi9N5gIuQL +i88NgGiBrErcMER9WhwEWW7pVbzyT2iBGsS1ksiSI4HBS4/HgxT2biFR7fhqoebOA a/uvgqlPTg+2eqWgWWvgg/dAxv508/ZAahdRuecKkrS9eVkJfHYw1rxqPgCTh9ZqS8 nsfvApb9SNsb3UfiltM4lJpeHveHjZSOok4t4dq4+DjHNrQlTCKo93ksePh8o+AOqZ ewaKtkTeIewC5JIsjg1xWAPVbr1y2z4NEFRNUGbRnrPYeGbNwATPTlEQ3WK4+8saov nhGT0LV7upPaw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1A83818005D for ; Tue, 26 Aug 2025 19:34:54 +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,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fout-a5-smtp.messagingengine.com (fout-a5-smtp.messagingengine.com [103.168.172.148]) (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 19:34:53 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.internal [10.202.2.50]) by mailfout.phl.internal (Postfix) with ESMTP id C859CEC01D0 for ; Tue, 26 Aug 2025 15:36:25 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-10.internal (MEProxy); Tue, 26 Aug 2025 15:36:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding: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=fm1; t=1756236985; x=1756323385; bh=8HsSPjFMa+H6VMUuQdJe2 jDeAeRrH9QUbZ8oZRSMloU=; b=D5h6jxXDTC2WfTn4dEkvUfk1E0s29YKyWVe3O /mzBkJkSWY2+G90tdZqlwwoduchNTMPPr4zXf7hchzL6RAbaipu7CmxuhQ3w2GJR ot859cekeYjc4jPcFYzYqM350W/P8O07kOoKv0N/lxnd9ongoPcKBJAYC2JDZPXC /K/ms9518PFwGTy/4Drkv+LnumYXCohYFVFwAEupug/qFHgNk0N7gOGhanvaPUjL 3QpNXAK+FVFpLdGYu3fhQ56OU6kbnpFR248gN6Ws9eh+zqsiDN9nrowTTZkeGqew ZU7KmjYcJhk8wZM2jEfPuZqS2rHpBGmAMIm95ZdeT2cfyYFzg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=1756236985; x=1756323385; bh=8 HsSPjFMa+H6VMUuQdJe2jDeAeRrH9QUbZ8oZRSMloU=; b=iCtQZtN6Cpxt8ydqw HiMeOoT9lPpqeJdSun81x7nukl99EMAo7ODEiXl/ulraw0wl+wdEURCNihB6Zo6d gHQMNyOb6pj9hAtL97KK/kymleapLA/M3YzUXClqjAPNZpE1bb9cnyKcA33fdO0m VV6GpvOK/9Jnbq78tEL5alIAw1aQx8s3Rw4LT04d6aVmjVcAIKHWRdLICH85jz0w KDYfooXLjm3FyztIL0rxzopW+388wDlY2OgffYeQ2VCnS0PeCltVPKTtJHe0kd3c 37RBPejp00i1pMByTUStYsgS8bDRPYaif6/jyPcVdkpfrtW0CxYvTS56VcIWBgNk TEoSQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgddujeeiuddvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepofggfffhvffkjghfufgtgfesthhqredtredtjeenucfhrhhomhepfdfnrghrrhih ucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmqe enucggtffrrghtthgvrhhnpeehjeefvefgfeduteffffdvheeiudekieefleevvdduiefg keehvdevheffvdegteenucffohhmrghinhepghhithhhuhgsrdgtohhmnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhf ihgvlhguthgvtghhrdgtohhmpdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpoh huthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 72C41700065; Tue, 26 Aug 2025 15:36:25 -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: AuIX-eojstS3 Date: Tue, 26 Aug 2025 14:36:05 -0500 To: "php internals" Message-ID: In-Reply-To: <642a1575-a855-442a-aa3c-3fa123e793d7@bastelstu.be> 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: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") 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 str= ing, should I cast it to an int or a float?", for which an is_representa= ble_as_int() function (or similar) would be quite helpful, and neater th= an the messy solution I usually use. > > It would've been nice to know what that use-case is, rather than just=20 > knowing that you had that use-case. > > I'm having a hard time thinking of something where I don't a-priori kn= ow=20 > what type I expect to get and would need to inspect the value to make = 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 error=20 > handling and taking only `string`s, could be useful, but that's not wh= at=20 > 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 of= ten 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?" =20 https://github.com/Crell/Carica/blob/master/src/Middleware/NormalizeArgu= mentTypesMiddleware.php#L73 "I have a string, the parameter wants an int, is that even possible?" B= eing able to replace that floor() nonsense with is_integerable() (by wha= tever name) would make my life a lot easier. For float, is_numeric() is already sufficient for my purposes. I just n= eed to be able to differentiate between "3" and "3.14" to cast to the co= rrect type. --Larry Garfield