Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128271 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 E38541A00BC for ; Mon, 28 Jul 2025 15:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753716640; bh=YKZ7spEeAmeb+fbacUl3i6tS04xTGHaBGI7EyAs0p5M=; h=Date:From:To:In-Reply-To:References:Subject:From; b=mprOmARbOFctCsbYIgaNyzwiTb/5U95+uqirz2h5CP1jbYI8Dgp3eoAFhCazz1ygY 75PvVuHniqACbFKMgADEprwNoZEZL6WsjrYFOon66+FnBpWGqCgJcehpK6RJKL2PJ8 juS/oMSgRmQXX+tzPbQHDGKJj5OAcYIvmyCqFd4O/KCnGQMycQ2XuRpgf+Isyd2AmJ 9NAMiH13pR22y9CnTn5rnnlwcHXbTjIhlJwgG5SZaNSkagblmKfRAjcfyvKAp/dR0R 5AlXRRhw4/lBCioWBQ/FtISr4dqCbIlgR5PY18yzAYH50eQMz0dtBCMRDtNF2daj36 EhPVE5qDI1p8Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0AFE11805D0 for ; Mon, 28 Jul 2025 15:30: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.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: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fhigh-a5-smtp.messagingengine.com (fhigh-a5-smtp.messagingengine.com [103.168.172.156]) (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 ; Mon, 28 Jul 2025 15:30:39 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfhigh.phl.internal (Postfix) with ESMTP id 8F72D1400049 for ; Mon, 28 Jul 2025 11:32:22 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-10.internal (MEProxy); Mon, 28 Jul 2025 11:32:22 -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=fm3; t=1753716742; x=1753803142; bh=mDtg/TlNOULxynd8p75to viUKZBqVDMNx+4IQ5sRvmg=; b=HXQAQtDJJINZhSJdzX1Irk+ZO2rs7yn0tcmWI MuOFoAoh1O1MlUWSdhgDzSlXRLWZ3vwYlH/lDg2gtDO45UuwhKj3+uJcu1oVNvRV 62rsEkcaai/GDx7lpQ7XKAiKKjq24C6Nw3OzmZIAevH7ZtwO++kqQTIjSui7sYu4 +cW4gs+pNBcVUq5sYzkplTLB2ILLeVizKzA7gGMugB2YrO6IpRIvdSuSnjfFE3F3 obg29m8YG6yqqW8ePzG3fO2MUbVp2xB+8x6XjCu+dTKJcg9Y8nfm9RxZlD7fOGNY bzmaFSNVLn9CChLc2Rj96tMPW2kAayL3dXlkzCdFeAAvT+7GA== 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=fm3; t=1753716742; x=1753803142; bh=m Dtg/TlNOULxynd8p75toviUKZBqVDMNx+4IQ5sRvmg=; b=kSK+Alt0I0n4gr63t AZAHwMlfxm1KxaMF5i8wM79wXe+ZcztxHuk/VucUxHhOMiCZkX2jR6+OdCuHx7NC w2rn1W3psvaiJ6cBWqz2NaOb3pn2+0IMflRMUD129QdkvNCLQ0zM9BLdVU81H0SJ EXIeoez32cCezP41gAE0epNY+iNXQtBB4rLO3FLFZ2o++RnGXvSxCAusPXnU9HNJ TnALCs0a0HKP1nOYWlkCzhfFVNOi/AnFR5/OEh9kJzeucf2zXp1zpCFDMvySa0Qy e7j0thCscxSVJIk0gmZWCuBOSsHPZXeyqNmQ8FpKpCG8WT4EussdPBY94DlToHJ+ cIahw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdelvdehgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggffhffvkfgjfhfutgfgsehtqhertdertdejnecuhfhrohhmpedfnfgrrhhrhicu ifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomheqne cuggftrfgrthhtvghrnhepffeiiedvhfdvgedutddtgeetieeugeevhfetheeffeeftedu iedthedtgeejueeinecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmpdhnsggprhgtphht thhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhsse hlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 5BD17700069; Mon, 28 Jul 2025 11:32:22 -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: T29c7413b2aec08ac Date: Mon, 28 Jul 2025 10:32:01 -0500 To: "php internals" Message-ID: <75b17924-d0d9-46e9-94d0-9c18d1cb9192@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] [DISCUSSION] Adding the "is_integer_safe()" function Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jul 28, 2025, at 8:00 AM, Alexandre Daubois wrote: > Le lun. 28 juil. 2025 =C3=A0 12:14, Gina P. Banyard a =C3=A9crit : >> For naming, maybe the following pair of functions make sense? >> - is_representable_as_int() > > Definitely, thank you for proposing. Having a pair of functions for > bidirectional transformations makes much more sense than a single > ambiguous function. Here are some quick specs for what these functions > could do. > > `is_representable_as_int(mixed $value): bool` checks if a given value > can be losslessly converted to an integer. > > - Floats: returns true if the float is within PHP_INT_MIN to > PHP_INT_MAX range AND has no fractional part > - Strings: returns true if the string represents a valid integer > within the platform's integer range > - Always returns true for integers > - Special float values: returns false for NaN, INF, -INF > > This gives the following example: > > ```php > is_representable_as_int(42.0); // true > is_representable_as_int(42.5); // false > is_representable_as_int(2**31); // false on 32-bit, true on 64-b= it > is_representable_as_int(2**63); // false on both platforms > is_representable_as_int("123"); // true > is_representable_as_int("123.0"); // true > is_representable_as_int("123.5"); // false > is_representable_as_int(NAN); // false > ``` Even without the precision loss question, such a function would be very = useful when parsing input data of unknown type (and thus is a string). = "Can I cast this string to an int to use with an int parameter without a= ny loss" is currently not as simple as it ought to me. Providing a sing= le standardized function for that would be most welcome. The name is quite long though. :-) >> - is_representable_as_float() The same argument applies here, although in most cases is_numeric() is c= lose enough. A more precise option would not be unwelcome, though. --Larry Garfield