PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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:
Note that in all arithmetic overflow cases, PHP will not abandon 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 2003 (c) HerongYang.com. All Rights Reserved. */ 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:
herong> \php\php NumericOverflow.php 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:
Exercise: Modify my sample script to find out the negative float value range.
Table of Contents
Introduction and Installation of PHP
►PHP Data Types and Data Literals
Data Literals Supported in PHP
Data Literals Examples for Integer, String and Other Data Types
►Overflow of Integer and Float Values
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS