[DISCUSSION] Adding the "is_integer_safe()" function
| From: | Alexandre Daubois | Date: | Fri, 25 Jul 2025 13:40:47 +0000 |
| Subject: | [DISCUSSION] Adding the "is_integer_safe()" function | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-128228@lists.php.net to get a copy of this message | ||
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