Integer Overflow & PHP_INT_MAX
debt(d8/e5/b4/t7)
Closest to 'silent in production' (d9), but PHPStan can flag some risky arithmetic patterns, so slightly better at d8. Silent float promotion means no error or warning at runtime.
Closest to 'touches multiple files / significant refactor' (e5). Switching from native arithmetic to BCMath strings requires changing every arithmetic operation, storage format, and comparison in the affected module.
Closest to 'localised tax' (b3) leaning toward persistent tax (b5), scored b4. Financial code carries ongoing BCMath verbosity, but it's typically scoped to billing/money components rather than system-wide.
Closest to 'serious trap' (t7). The misconception states devs expect overflow errors but get silent float promotion — contradicts behavior of strongly-typed languages and silently corrupts precision in financial code.
TL;DR
Explanation
PHP_INT_MAX on 64-bit systems is 9223372036854775807. Exceeding it silently converts to float, losing precision. PHP_INT_MIN is the negative limit. This is particularly dangerous for: financial calculations, Unix timestamps post-2038 on 32-bit, large ID generation, cryptographic computations. Solutions: use bcmath extension (bcadd, bcmul etc.) for arbitrary precision, GMP for large integer arithmetic, or the PHP 8.2+ native support for large numbers via numeric strings. Never use floating-point for money — use integer cents or BCMath.
Common Misconception
Why It Matters
Common Mistakes
- Using regular PHP arithmetic for financial calculations — use BCMath with string representation.
- Assuming 32-bit PHP_INT_MAX (2147483647) on modern 64-bit servers.
- Using float for currency — always use integer cents or BCMath strings.
Code Examples
$price = 9999999999999999; // Exceeds float precision
$tax = $price * 0.2;
echo $tax; // Incorrect: float precision loss
// BCMath for precise decimal arithmetic:
$price = '9999999999999999';
$tax_rate = '0.20';
$tax = bcmul($price, $tax_rate, 2); // '1999999999999999.80'
// PHP_INT_MAX check:
if ($value > PHP_INT_MAX) {
throw new \OverflowException('Value exceeds integer range');
}