Forum Moderators: coopster

Message Too Old, No Replies

testing post variables to be an integer

         

ryan_b83

11:18 pm on Dec 14, 2006 (gmt 0)

10+ Year Member



I am trying to test to see if a posted value is an integer. However apparently the is_int() function dosn't work like the is_numeric() function when testing a varible that is a string....

For example i did a var_dump() on a post value and it came up

STRING("3");

if i did:

is_numeric($_POST[value]); //this returns a true because "3" is numeric

However if i did

is_int($_POST[value]); //this returns false

What is the fastest way to test a post/get value if its an integer?

Thanks,
Ryan

eelixduppy

11:25 pm on Dec 14, 2006 (gmt 0)



Form data is always a string.


$pattern = "/^([0-9])+$/";
if(preg_match($pattern,$string)) {
echo 'good int';
} else {
echo 'not an int';
}

FalseDawn

11:31 pm on Dec 14, 2006 (gmt 0)

10+ Year Member



Alternatively, a function I use that is probably a bit more efficient than using a regex:


function CheckIsInt ($x)
{
return (is_numeric($x)? intval($x)==$x : false);
}

eelixduppy

11:34 pm on Dec 14, 2006 (gmt 0)



nvm

coopster

11:10 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The function listed is indeed slightly more efficient than a regular expression -- you have to get down to the hundred-thousandths of a second though, likely nobody is going to notice ;)

However, what you will notice is if you try to use an integer value outside of the following range on a 32-bit system:

-2147483648 to 2147483647

The intval() function will fail you. You see, anything over 10 digits (on 32 bit systems) is no longer an integer in PHP, it's a float. There is also a max range on 64-bit systems (see documentation references below).

Also be careful with is_int() [php.net]! You'll run into the same issues. More information on this can be found on the intval() [php.net] and the integer [php.net] pages.

What will work? Well, I ran into this a couple of years ago and have found the following function to be more reliable for validating integers:

function isValidInteger($i) 
{
return (is_numeric($i) && !preg_match('/[[:^digit:]]/', $i) ? true : false);
}

Here is a snippet to test on your own system. I ran this one on a 32-bit system to demonstrate the output:

<pre>
<?php
function CheckIsInt ($x)
{
return (is_numeric($x) ? intval($x)==$x : false);
}
function isValidInt($i)
{
return (is_numeric($i) && !preg_match('/[[:^digit:]]/', $i) ? true : false);
}
$integers = array(
123456789,
2147483647,
2147483648
);
foreach ($integers as $i) {
print 'isValidInt('.$i.'): ';
var_dump(isValidInt($i));
print 'CheckIsInt('.$i.'): ';
var_dump(CheckIsInt($i));
}
?>
<pre>

Gives the following output:
isValidInt(123456789): bool(true) 
CheckIsInt(123456789): bool(true)
isValidInt(2147483647): bool(true)
CheckIsInt(2147483647): bool(true)
isValidInt(2147483648): bool(true)
CheckIsInt(2147483648): bool(false)

The emphasis on the last printed value shows where the intval() function will fail you. Just a heads up ;)