Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Sponsored Links

Bypassing PHP 32 Bit Integer Bounds


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];

// 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];

// 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

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  radalin (#58095)
Date: Fri Jan 29, 2010. 09:30:17

Post #1 of 1

there is much simpler way however in PHP.

you can use bc* functions for operations with large numbers instead of making a select operation.

bcsub("123123123123123132123123123213123123", 1231231231231321231231231231231329);

it will return the subtraction of operation.

Reply to this post


--- View Entire Thread ---
Amazing Font Pack!

Featured Tutorialsmore

The Plastic Effect
The Plastic Effect
- Adobe Photoshop -
Imageready Interfa...
Imageready Interfa...
- Adobe Photoshop -
LCD Interfaces
LCD Interfaces
- Adobe Photoshop -
StarBurst Effects
StarBurst Effects
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Advertisements





Special Options
Printer Friendly Version
Forum Threads

 Re: 3ds Max Tutorials for Beginners
Author: 3DSMaxresources
Posted: Feb 22nd, 4:29pm
Activity: 0 replies, 856 views
Delete Account
Author: Neo824
Posted: Oct 18th, 7:47am
Activity: 1 replies, 1922 views
Back...
Author: unleash
Posted: Jul 02nd, 12:37pm
Activity: 2 replies, 2034 views
Help Please :)
Author: Roosta
Posted: Mar 25th, 5:08am
Activity: 0 replies, 2481 views
thank you
Author: HypepapyHer
Posted: Mar 24th, 9:18pm
Activity: 1 replies, 1802 views
 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 12:16pm
Activity: 1 replies, 2557 views
 changes....
Author: supertackyman
Posted: Sep 12th, 3:56am
Activity: 2 replies, 3537 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 4:50pm
Activity: 0 replies, 2721 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 1:58pm
Activity: 0 replies, 4093 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 3:18pm
Activity: 1 replies, 3877 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 3:33pm
Activity: 0 replies, 17231 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 4:37am
Activity: 0 replies, 4972 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4669