number_format() Validates Its Decimals Argument Range¶
number_format()’s $decimals argument is internally clamped to a 32-bit signed integer range (-2147483648 to 2147483647). Until PHP 8.6, a value outside that range was silently clamped, for very negative values this collapsed to 0 decimals, and for very large positive values it could trigger a huge memory allocation while building the result string. In PHP 8.6, an out-of-range value throws a ValueError instead.
PHP code¶
<?php
try {
var_dump(number_format(1234.5678, PHP_INT_MIN));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
?>
Before¶
string(1) 0
After¶
number_format(): Argument #2 ($decimals) must be between -2147483648 and 2147483647
PHP version change¶
This behavior changed in 8.6