php check to see if variable is integer
try filter_var
function
filter_var($_POST['id'], FILTER_VALIDATE_INT);
use:
if(filter_var($_POST['id'], FILTER_VALIDATE_INT)) {
//Doing somethings...
}
Using is_numeric()
for checking if a variable is an integer is a bad idea.
This function will send TRUE
for 3.14
for example. It's not the expected behavior
To do this correctly, you can use one of these options :
Considering this variables array :
$variables = [
"TEST 0" => 0,
"TEST 1" => 42,
"TEST 2" => 4.2,
"TEST 3" => .42,
"TEST 4" => 42.,
"TEST 5" => "42",
"TEST 6" => "a42",
"TEST 7" => "42a",
"TEST 8" => 0x24,
"TEST 9" => 1337e0
];
The first option (FILTER_VALIDATE_INT Way) :
# Check if your variable is an integer
if( ! filter_var($variable, FILTER_VALIDATE_INT) ){
echo "Your variable is not an integer";
}
Output :
TEST 0 : 0 (type:integer) is not an integer ✘
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔
The second option (CASTING COMPARISON Way) :
# Check if your variable is an integer
if ( strval($variable) != strval(intval($variable)) ) {
echo "Your variable is not an integer";
}
Output :
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔
The third option (CTYPE_DIGIT Way) :
# Check if your variable is an integer
if( ! ctype_digit(strval($variable)) ){
echo "Your variable is not an integer";
}
Output :
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔
The fourth option (REGEX Way) :
# Check if your variable is an integer
if( ! preg_match('/^\d+$/', $variable) ){
echo "Your variable is not an integer";
}
Output :
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔
try ctype_digit
if (!ctype_digit($_POST['id'])) {
// contains non numeric characters
}
Note: It will only work with string
types. So you have to cast to string
your normal variables:
$var = 42;
$is_digit = ctype_digit((string)$var);
Also note: It doesn't work with negative integers. If you need this you'll have to go with regex. I found this for example:
EDIT: Thanks to LajosVeres, I've added the D modifier. So 123\n
is not valid.
if (preg_match("/^-?[1-9][0-9]*$/D", $_POST['id'])) {
echo 'String is a positive or negative integer.';
}
More: The simple test with casting will not work since "php" == 0 is true
and "0" === 0 is false
!
See types comparisons table for that.
$var = 'php';
var_dump($var != (int)$var); // false
$var = '0';
var_dump($var !== (int)$var); // true
In PHP $_POST
values are always text (string
type).
You can force a variable into the integer type like this:
$int_id = (int)$_POST['id'];
That will work if you are certain that $_POST['id']
should be an integer. But if you want to make absolutely sure that it contains only numbers from 0
to 9
and no other signs or symbols use:
if( ctype_digit( $_POST['id'] ) )
{
$int_id = (int)$_POST['id'];
}