The float %.*H is not representable as an int, cast occurred¶
Description¶
When a float is cast to int – explicitly with (int)/intval(), or implicitly wherever an integer is required, such as an array offset or a bitwise operator – and the value is outside the range representable by PHP_INT_MIN/PHP_INT_MAX, or is NAN or INF, PHP cannot produce a meaningful integer.
Rather than silently returning an arbitrary or platform-dependent bit pattern, PHP emits this warning and returns 0 for NAN/INF, or a value obtained by taking the float modulo the 64-bit integer range for very large finite values – in both cases, a value with no reliable relationship to the original float.
Example¶
<?php
var_dump((int) 1.0E+20);
// int(0) on 64-bit, and the value bears no relation to 1.0E+20
var_dump((int) NAN);
// int(0)
var_dump((int) INF);
// int(0)
?>
Literal Examples¶
The float 1.0E+20 is not representable as an int, cast occurred
The float NAN is not representable as an int, cast occurred
The float INF is not representable as an int, cast occurred
Solutions¶
Check the float against
PHP_INT_MIN/PHP_INT_MAX(and againstis_nan()/is_infinite()) before casting it.Keep the value as a
floator as a string if it may exceed the integer range.Use
BCMathorGMPif arbitrary-precision integers are required.
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.