PHP Integers

Integers. You mean math?

The integer data type represents whole numbers, either positive or negative or zero, not fractions. Before we get into anything worth looking at, let’s look at a simple example. Create a new PHP file and copy the following code into it.


$x = 0;
$y = 1;
$z = -2;echo $x + $y + $z;?>


You should get a -1 as the result. I know that we haven’t looked at arithmetic operators, but I’m sure that you can figure out what the + operator does in the previous example. We set three variables, $x, $y, and $z to 0, 1, and -2, added them, and echoed them out.

Now that we’ve looked at a simple example, let’s go over a few details as to why the integer is an important topic to discuss. It’s not important in just PHP, it’s important in any programming language.

PHP integers have a maximum and minimum value for both the 32 bit and the 64 bit operating systems.

32 bit Operating System

Minimum value: -2147483647
Maximum value: +2147483647

Why does this matter? Because your results might differ from what you intended. We’ll set $x equal to the maximum integer value, 2147483647. We’ll set $y equal to the maximum integer value + 1, which is 2147483648.


$x = 2147483647;
$y = 2147483648;echo "X: " . $x . "<br>";
echo "Y: " . $y . "<br>";?>


Depending on what type of computer you have, you’ll either see the correct values, or something weird is going to happen. I’m running on a 64 bit operating system, so the values will display correctly.

X: 2147483647
Y: 2147483648

64 bit Operating System

Minimum value: -9223372036854775807
Maximum value: +9223372036854775807

We’ll repeat the same process; we’ll set $x equal to 9223372036854775807 and $y equal to the maximum integer value for the 64 bit operating system plus 1, 9223372036854775808.


$x_64 = 9223372036854775807;
$y_64 = 9223372036854775808;echo "X-64: " . $x . "<br>";
echo "Y-64: " . $y . "<br>";?>


If you’re running on the 64 bit platform, what did you get? The result for $x displayed properly but the result for $y did not.

X-64: 9223372036854775807
Y-64: 92233720368548E+18

The maximum value exceeded the storage capacity for the integer data type, and was automatically converted to a double. A double allows you to store integers and decimal type numbers and has a larger storage capacity than an integer data type. We can test to see what data types are stored inside each variable with the assistance of the gettype() PHP function.


$x = 2147483647;
$y = 2147483648;
$x_64 = 9223372036854775807;
$y_64 = 9223372036854775808;echo "X32 Data Type: " . gettype($x) . "<br>";
echo "Y32 Data Type: " . gettype($y) . "<br>";
echo "X64 Data Type: " . gettype($x_64) . "<br>";
echo "Y64 Data Type: " . gettype($y_64) . "<br>";?>


If you’re running on a 64 bit operating system, you’ll notice that $x, $y, and $x_64 are integers, and $y_64 is a double.

X32 Data Type: integer
Y32 Data Type: integer
X64 Data Type: integer
Y64 Data Type: double

When are you likely to experience this issue? Most PHP applications will have some sort of database access. We’ll cover databases much later, but for now, think of them like excel sheets. You can store data into them and then retrieve information out of them. You’ll specify what type of data is to be stored in each cell and the database constraints access to that cell. If you specify that you’re storing an integer, and you try to store a double, it will cut off any decimal points from the number.

Based on the questions that constantly pop up in online discussion boards, I would have to argue that quite a few developers have specified that they will be storing an integer value when storing phone numbers inside their database.

You proceed to add thousands of phone numbers to the database and you quickly realize that majority of them have been converted to 2147483647. If you didn’t know that your database used the 32 bit architecture, you’ll be at a complete loss.

Look at the maximum integer value for 32 bit systems; it resembles a phone number.


If you try to add a phone number like 5555555555, you will see that the number that’s actually stored is 2147483647. And yes, that’s really bad. To make sure that doesn’t happen, look up the maximum value for the data type that you’re setting for that specific cell inside the database, and verify that it can actually support your input.




Continue your learning with these articles

PHP Truthy Bools

What Do You Mean Truthy?


Booleans represent true or false. PHP has truthy booleans, which are values that are not true or false, but when evaluated inside an expression, they can act like they are.

PHP Integers

You Know, Integers. Like Math.

PHP – P6: Integers

The integer data type represents whole numbers, either positive or negative or zero, not fractions.

PHP Floats - Doubles



Floats and doubles are the same thing in PHP. Floating points in PHP have precision for up to 14 digits. That includes the cumulative digits, before and after the period.

Leave a Reply