On Friday, 25 July 2025 at 14:43, Alexandre Daubois <alex.daubois+...@gmail.com> wrote:
> Hello Internals, > > I would like to gather feedback on the proposal to add an > "is_integer_safe()" function to PHP. The idea came to me while > replying to a thread [1], but I think creating a discussion is more > appropriate. > > Here are a few reasons I think this would be useful: > > - PHP automatically converts integers that exceed PHP_INT_MAX to > float, which can cause undetected precision loss. This function would > allow checking that an integer remains within safe bounds before > critical operations ; > - Integer limits vary between 32-bit and 64-bit systems. This function > would provide a standardized way to check the portability of integer > values across different architectures ; > - This would improve code readability. We had the case in Symfony [2] > to implement a JSONPath crawler, and the solution feels hacky to say > the least ; > - PHP already has functions like `is_finite()` and `is_nan()` for > floats. Adding `is_integer_safe()` would logically complete this set > of type validation functions. > - Of course more optimized than dealing with strings and using > arithmetic operators on strings ; > - As PHP continues to evolve towards stricter type safety > (strict_types, typed properties, etc.), having more granular type > validation functions supports this direction. > > If this proposition gets some interest, I'll take care of the > implementation in the engine. I guess it would also require an RFC. > > Best, > Alex > > [1] https://externals.io/message/128032 > [2] > https://github.com/symfony/symfony/blob/d3a0df0243e1c68598fa066eaa6cd0cf39f256dc/src/Symfony/Component/JsonPath/JsonPathUtils.php#L243 Naming aside, I would be very much in favour of anything that makes it easier to make bidirectional transformations between floats and ints reliable. For naming, maybe the following pair of functions make sense? - is_representable_as_int() - is_representable_as_float() Best regards, Gina P. Banyard