5

What is the main purpose for the maximum limit for number in php and mysql?
Does this means that we can/cannot process or store numbers that is larger than the limit?

I echo PHP_INT_MAX and it shows 2147483647.

I multiply by 1000000000000000000000 and get answers like 2.147483647E+30 which i think is already over the limit?

Please advise.

Many thanks for guidance.

This question arise when I'm thinking about validating the user form input. I want to make sure that the user input is according to our defined number format and not something else.
Then i do some online search for best practices and come to aware of this "limits" but do not know how to handle it correctly when using PHP & MYSQL. Pls advise:

Step 1: trim and convert user form input "string number" to number.
Step 2: validate the number is in positive integer format.
Step 3: validate the number does not exceed my "max limit".
Since php limit (2,147,483,647) is smaller than mysql limit (18,446,744,073,709,500,000)? i'll take php as my max limit.
Step 4: perform some calculations...
Step 5: validate my result does not exceed my max limit.
Step 6: store the result in mysql.

user1884324
  • 693
  • 5
  • 14
  • 21
  • Which number limit? Do you mean, for instance, that a integer variable can only hold numbers to a certain value? – michaelward82 Sep 26 '13 at 09:07
  • The purpose is filling less bytes. An unsigned tinyint is only 8 bits long (1 byte), and it's highest value is only 255 (11111111), whereas the more bytes we use, the more memory we use. – h2ooooooo Sep 26 '13 at 09:07
  • It's because numbers are typicallys stored as 32-bit or 64-bit signed/unsigned integers or floats, so they're limited by the number of bits used to store them – Mark Baker Sep 26 '13 at 09:07
  • 2.147483647E+30 is not an integer, so it's not subject to the restrictions for integers. – Jon Sep 26 '13 at 09:08
  • [**Related ...**](http://stackoverflow.com/questions/18046347/unexpected-results-when-working-with-very-big-integers-on-interpreted-languages) – HamZa Sep 26 '13 at 09:14
  • It's been a few days and you're unlikely to attract any more answers at this stage. Did someone answer your question adequately? If so, please mark the answer as correct. Thanks. – michaelward82 Sep 30 '13 at 19:25

3 Answers3

3

It's hardware limit of the CPU. You can still use bc math function to work with larger numbers. In mysql it is about aligning bytes, so it knows at which offset is which column.

The multiply result is converted to float.

Marek
  • 7,337
  • 1
  • 22
  • 33
1

PHP_INT_MAX is the constant of the largest integer you can use on this build of PHP (32 bit in your case). If your OS and CPU support 64 bit, you can use a 64 bit build of PHP to support a much larger number in an integer. (This causes problems where developers have designed their code around the limits of a 64 bit build and is then used on 32 bit builds, assuming the type matters.)

When you multiply the number by a larger one, PHP recognises this new value is not going to fit in an integer and converts the type to a long or float. In the latter case, you would lose some precision, so it's important you're careful when considering how your code affects variable types. In some languages, you would receive an error for trying to set a value larger than was allowed by that type, because the language would refuse to change the type automatically for you. In this way, PHP is a more basic programming language to use.

<?php
$my_number = PHP_INT_MAX;
var_dump(gettype($my_number), $my_number);
$my_number = $my_number * 1000000000000000000000;
var_dump(gettype($my_number), $my_number);

Output:
string(7) "integer"
int(2147483647)
string(6) "double"
float(2.147483647E+30)
deed02392
  • 4,799
  • 2
  • 31
  • 48
0

In the world of computing, there are many limits based upon the mathematical model of the computer hardware we use.

For instance, if we decided to represent an integer number in 1 bit of memory, then we would be able to represent the numbers 0 and 1.

If we were to increase that to the more common values of 8, 16, 32 or 64 bits then we can represent the following number of distinct values:

  • 2^8 - 256,
  • 2^16 - 65,536,
  • 2^32 - 4,294,967,296,
  • or 2^64 - 18,446,744,073,709,551,616.

Of course, if we wish to be able to represent negative numbers then we can sign the integer (use one bit to indicate negative or positive). In the case of a 32 bit signed integer this would allow us to represent the numbers: −(2^31) to 2^31 − 1 or -2,147,483,648 to +2,147,483,647 (the same upper limit as the number in your question).

Integers are arguably the simplest form of number representation in a computer system (beyond straight binary), but because of the inherent limits of the system, we will often need to use other systems for larger numbers that we cannot represent with an integer. PHP will switch from using an integer to using floating point numbers when the limit is exceeded.

The limit you are seeing in PHP is compiled in, and will depend upon the architecture of the compiler. It looks as if your PHP was compiled as 32 bits.

You can read up far more on computer number systems on Wikipedia.

michaelward82
  • 4,706
  • 26
  • 40