 |

Ask yourself this simple question - Have you every found yourself attempting to calculate a very large number
but found you were getting inaccurate results because PHP could not handle it and was returning a float
instead of an integer?
Here is an example:
Example 1: Very Large Number
( 3000000000 * 1000000000 ) / ( 1024 * 365000 )
If this case, or a similar case applies to you, then read on for a neat and useful trick that can come in
handy when attempting to perform large calculations such as this. You're going to need a basic knowledge
of mySQL for this tutorial.
PHP's Integer Limitations
PHP handles integers based on the platform it is running on. For 32 bit systems the max value for any
integer you can have is between -2,147,483,647 and 2,147,483,647.
These are known as the integer bounds and if you attempt to assign a value
outside these bounds, PHP will convert it to a float.
Limitation Problems
You might be asking yourself right about now who in the world is going to attempt to calculate
a number bigger than 2.1 Billion in their PHP script or application!
The answer actually depends. Not a lot of people might but there are a select few who just might
need to.
In my case for example, I was working on a script to calculate bandwidth consumption on a
very large network. The bandwidth was calculated in bytes so the numbers began getting
a little huge.
The bigger they got, I found PHP was converting them to floats and I was getting inaccurate
results.
Overcoming The Problem
Thankfully there is a very simple solution to avoid a similar problem. Enter mySQL to the
rescue. Unlike PHP, mySQL handles integers a little differently and because of that, supports
very large numbers.
For signed integers, mySQL has max bounds between -9,223,372,036,854,775,807 and
9,223,372,036,854,775,807. For unsigned integers, these bounds shift from
0 to 18,446,744,073,709,551,615.
When we say max bounds, we refer to the different integer sizes mySQL can handle.
Like PHP, mySQL also supports 32 bit integers but for the arithmetic operations it performs,
see next section, it always uses 64 bit integers, hence the max bounds
we just mentioned.
What does this mean for us? Basically we just overcame our problem because not only can
mySQL store data, it can also calculate data for us.
mySQL's Arithmetic Functions
Like just pointed out, mySQL has basic arithmetic capabilities. mySQL can add, subtract, multiply
and divide quite easily. All you would have to do is provide the equation you want to
calculate in a Select statement and query the server.
Let's provide an example of basic arithmetic calculations using mySQL:
Example 2: mySQL Add
<?php
// Create Query.
$query = "Select 8 + 5";
// Issue Query.
$queryResult = mysql_query( $query );
// Get Result Set. There is no need to loop
// Like you normally would in a result set
// Because artimetic operations always
// Return 1 ROW -- The Result.
$queryContent = mysql_fetch_row[ 0 ];
// Print Result. Should Print 13.
echo $queryContent;
?>
Pretty simple. If you even know the basics of querying mySQL from PHP, then this code is extremely
simple and easy to understand.
Of course you should not use mySQL for such simple calculations; instead use PHP. You should only
opt to use mySQL in situations such as the one provided in Example 1. Lets rewrite Example 1 to allow mySQL to perform the calculation for us.
Example 3: A Huge Calculation
<?php
// Create Query.
$query = "Select ( 3000000000 * 1000000000 ) / ( 1024 * 365000 )";
// Issue Query.
$queryResult = mysql_query( $query );
// Get Result Set. There is no need to loop
// Like you normally would in a result set
// Because artimetic operations always
// Return 1 ROW -- The Result.
$queryContent = mysql_fetch_row[ 0 ];
// Print Result.
echo $queryContent;
?>
Closing Notes
A couple of things to consider carefully before ending this tutorial. PHP always interprets anything
returned from mySQL, such as the examples we provided, as strings. That's why the result returned
to you by mySQL is not truncated nor converted because PHP interprets it a a string not
an integer. And the limit for the number of characters in a string is, well, pretty huge; don't
try to count.
Because PHP interprets this a string, it can cause several problems for you. First you can not
use the result in another calculation in your script unless you want a risk of truncation or
type casting.
PHP has automatic type casting, in that if you use a numeric string in an arithmetic calculation
PHP will convert it to a integer. If the conversion is too big and outside the bounds of PHP, it will
then convert it yet again to a float. Good luck having accurate results now.
If you want to perform a large calculation to simply just print out the result or to save it for later use,
this method is for you. If your even willing to calculate the same result using mySQL all the time, then
once again this method is for you.
Conclusion
In this tutorial we learned about integer bounds, how PHP converts integers, and how to bypass
the integer bounds using a simple trick via mySQL.
As a closing note, we hope you enjoyed this tutorial and negative remarks are not appreciated. We tried
this and yes we think it is a good idea. If you have a better idea, we would love to hear it on our
forums.
- Tutorial written by Limitless
|  |