Integers and Relativity in PHP

12 03 2013

What is universally true about integers in PHP is that they constitute whole numbers and their representations include a range of formats, such as hexadecimal, decimal, octal and even binary since PHP5.4. Surprisingly, the range of numbers that PHP’s integers encompass is a relative matter. If you are on a 32-bit box running a 32 bit compiled version of PHP, then PHP will store only the numeric values approximating -2 billion to 2 billion. If you work on a 64-bit platform, but your operating system (OS) restricts integers to 32 bits, then you will need to use a 32-bit compiled version of PHP. Windows is one OS that has typically had such a restriction. If your hardware supports a 64 bit integer and your OS is Linux, then the range of integers will be far more expansive, roughly -9 quintillion to 9 quintillion. How do these disparate integer ranges result? The answer lies in PHP’s compilation.

In a Linux setup, there is a C source code file called limits.h; issue a whereis limits.h command at the command line to discover its location. The file defines various C constants, including one called LONG_MAX, as per the following code snippet:

# if __WORDSIZE == 64
# define LONG_MAX 9223372036854775807L
# else
# define LONG_MAX 2147483647L
# endif

As you may note, the wordsize of a system, i.e. the number of bits that a CPU can process as a unit of data (see Word (computer_architecture)) influences the constant’s value. The above code enables the constant LONG_MAX to be set when a developer compiles PHP (see Pierre Joye’s comment at github).

Why should a PHP user care about the C constant LONG_MAX? PHP internally uses that constant to create a constant for users, PHP_INT_MAX (see What can we learn from this? Integers in PHP are the equivalent of long integers in C, i.e. C89, a version of C that PHP has adhered to by catering to “the lowest denominator” rather than switch to C99 (see Rasmus Lerdrof’s explanation at

A long integer in the C Programming language has the advantaged of being able to represent a signed numerical value that is greater than or equal to an int. This flexibility allows the PHP integer to exist on either a 32-bit platform with a maximum value of 2147483647 or 9223372036854775807 on a 64-bit platform. (For more info about the subtleties of ints, long ints and long long ints, see StackOverflow).

The C file limits.h additionally removes any uncertainty about what constitutes an int in the C language itself by defining a maximum value of 2147483647 which results in PHP’s internal source code for an int and a long having respectively different maximum values on a 64-bit platform. So, compilation adds more certainty as to what numerical values a developer may wish to store as ints and which as longs for a given PHP configuration. Thus all PHP’s integers are longs and these data types may even for some setups be equivalent to the basic numeric type int.

Should the LONG_MAX constant not be defined in limits.h (if the file were missing or its content altered), various source code files define it frequently assigning it a default of approximately 2 billion, as follows:

#ifndef LONG_MAX
#define LONG_MAX 2147483647L

You may wish to run the following script to gauge the range of PHP integers a particular system supports:

echo "Maximum Integer Value: ", PHP_INT_MAX, "\n";
echo "Minimum Integer Value: ", ~PHP_INT_MAX,"\n";

Or, you might wish to inspect how many bytes constitute an integer  by running the following script:

echo "Integer Size in Bytes: ", PHP_INT_SIZE;

PHP_INT_SIZE indicates the size of a long (short-hand for long integer) on your system. If you see 4 that means 32 bits (4 bytes * 8 bits/byte) and 8 bytes == 64 bits.

This work is licensed under a Creative Commons License



2 responses

24 07 2013
17 09 2013

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: