PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Overflow of Integer and Float Values

This section describes what an arithmetic overflow condition, how the PHP 5 engine handles integer and float value overflow, how to write a PHP code to detect overflow conditions.

What is overflow? Overflow or arithmetic overflow is a condition that occurs when a calculation produces a result that is greater in magnitude than what a given data type can store or represent.

When applying this definition to PHP integer data type, we can say that: PHP integer value overflow is a condition when an integer operation produces an integer outside the signed 32-bit integer value range.

When applying this definition to PHP float data type, we can say that: PHP float value overflow is a condition when a float operation produces a float value outside the 64-bit floating point number value range.

According to the PHP manual, here is how the PHP 5 engine handles arithmetic overflow conditions:

  • When integer value overflow happens at the upper limit of the integer value range, the resulting integer value will be automatically converted to float data type.
  • When integer value overflow happens at the lower limit of the integer value range, the resulting integer value will be automatically converted to float data type.
  • When float value overflow happens at the upper limit of the exponent value range, the resulting float value will be automatically converted to a special value called "infinite".
  • When float value overflow happens at the lower limit of the exponent value range, the resulting float value will be automatically converted to 0. This is also called underflow.

Note that in all arithmetic overflow cases, PHP will not abend the operation and raise any runtime errors. So if you are writing a PHP code dealing with numbers that close to integer or float value range limits, you need to be very careful and write extra code to detect overflow conditions yourself.

To help us understand what are integer and float value range and how to detect arithmetic overflow conditions, I wrote this tutorial sample PHP script:

<?php /* NumericOverflow .php
* Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
*/
print "\n Signed 32-bit integer positive overflow test:\n";
$integerLimit =  0x7FFFFFFF; 
$integerOverflow = $integerLimit + 1;
print "    Integer upper limit: "; var_dump($integerLimit);
print "   Upper limit overflow: "; var_dump($integerOverflow);

print "\n Signed 32-bit integer positive overflow test:\n";
$integerLimit =  -0x7FFFFFFF - 1;
$integerOverflow = $integerLimit - 1;
print "    Integer lower limit: "; var_dump($integerLimit);
print "   Lower limit overflow: "; var_dump($integerOverflow);

print "\n 64-bit floating point number overflow test:\n";
$floatLimit = 1.0;
$floatOverflow = $floatLimit*10;
while (!is_infinite($floatOverflow)) {
   $floatLimit *= 10;
   $floatOverflow = $floatLimit*10;
}	
print "   Float high limit: "; var_dump($floatLimit);
print "     Float overflow: "; var_dump($floatOverflow);

print "\n 64-bit floating point number underflow test:\n";
$floatLimit = 1.0;
$floatUnderflow = $floatLimit/10;
while (!$floatUnderflow==0) {
   $floatLimit /= 10;
   $floatUnderflow = $floatLimit/10;
}
print "   Float low limit: "; var_dump($floatLimit);
print "   Float underflow: "; var_dump($floatUnderflow);
?>

If you run this sample script, you should get:

 Signed 32-bit integer positive overflow test:
    Integer upper limit: int(2147483647)
   Upper limit overflow: float(2147483648)

 Signed 32-bit integer positive overflow test:
    Integer lower limit: int(-2147483648)
   Lower limit overflow: float(-2147483649)

 64-bit floating point number overflow test:
   Float high limit: float(1.0E+308)
     Float overflow: float(INF)

 64-bit floating point number underflow test:
   Float low limit: float(9.88131291682E-324)
   Float underflow: float(0)

The result tells me that on my computer:

  • PHP integer value range is from -2147483648 to 2147483647.
  • PHP float value range is from 9.88131291682E-324 to 1.0E+308 approximately.
  • Of course, float values have another valid range on the negative side. It should be from -1.0E+308 to -9.88131291682E-324 approximately.

Exercise: Modify my sample script to find out the negative float value range.

Last update: 2005.

Sections in This Chapter

Data Types Supported in PHP

Data Literals Supported in PHP

Data Literals Examples for Integer, String and Other Data Types

Overflow of Integer and Float Values

Dr. Herong Yang, updated in 2009
Overflow of Integer and Float Values