Handling very large numbers with php. Fastest function?
<p>I'm trying to find out the best way to handle very large numbers. The number should always be positive and return 0 if invalid.</p>

<p>Here's how I've been testing different functions:</p>

<p>The results with n=999999999999999</p>

<pre><code>1) Result [1.0E+15]: 0.0921649932861 sec.
2) Result [999999999999999]: 0.0534319877625 sec.
3) Result [999999999999999]: 0.0666420459747 sec.
4) Result [999999999999999]: 0.212832927704 sec.
5) Result [999999999999999]: 0.0403189659119 sec.
6) Result [999999999999999]: 0.0412750244141 sec.



function int1($i)
{
    $i = preg_replace('/[^\d]/', '', $i);
    if (!strlen($i))
        $i = 0;
    return $i;
}


function int2($i = 0)
{
    $i = $i + 0;
    return $i;
}


function int0($i)
{
    $i = floor($i);
    $i = ($i &lt; 0) ? ($i * -1) : $i;
    return $i;
}


// test 1
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
    $tmp = int0($x);
} while ($i++ &lt; 100000);
$end = microtime(true);
echo '1) Result [' . $tmp . ']: ' . ($end - $start) . ' sec.&lt;br /&gt;';

// test 2
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
    $tmp = abs($x);
} while ($i++ &lt; 100000);
$end = microtime(true);
echo '2) Result [' . $tmp . ']: ' . ($end - $start) . ' sec.&lt;br /&gt;';

// test 3
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
    $tmp = float($x);
} while ($i++ &lt; 100000);
$end = microtime(true);
echo '3) Result [' . $tmp . ']: ' . ($end - $start) . ' sec.&lt;br /&gt;';


// test 4
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
    $tmp = int1($x);
} while ($i++ &lt; 100000);
$end = microtime(true);
echo '4) Result [' . $tmp . ']: ' . ($end - $start) . ' sec.&lt;br /&gt;';


// test 5
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
    $tmp = intval($x);
} while ($i++ &lt; 100000);
$end = microtime(true);
echo '5) Result [' . $tmp . ']: ' . ($end - $start) . ' sec.&lt;br /&gt;';

// test 6
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
    $tmp = int2($x);
} while ($i++ &lt; 100000);
$end = microtime(true);
echo '6) Result [' . $tmp . ']: ' . ($end - $start) . ' sec.&lt;br /&gt;';
</code></pre>

<p>I'm not sure why intval returns that big numbers on my local server, but it shouldn't work like that.</p>

<p>$i + 0; seems to be the best method.</p>

<p>What's the best/fastest way to treat large numbers before DB? </p>
