I am posting numeric value by a form and in php file use var_dump(is_int($my_var)); but it return bool(false) though i am posting 1 and if I use var_dump(is_int(1)); it is fine and return bool(true)
what is wrong....?
2 Answers
Variables transmitted by a POST request are strings, so you're calling is_int() on a string which returns false.
You may use is_numeric() or filter_var() instead or simply cast your variable to an integer.
Comments
// First check if it's a numeric value as either a string or number
if(is_numeric($int) === TRUE){
// It's a number, but it has to be an integer
if((int)$int == $int){
return TRUE;
// It's a number, but not an integer, so we fail
}else{
return FALSE;
}
// Not a number
}else{
return FALSE;
}
Also, instead of getting the variable as
$my_var = $_POST["value"];
try this instead to see if the value is really passed.
$my_var = $_REQUEST["value"];
1 Comment
KingCrunch
You can reduce your whole code snippet to
return (int)$int == $int;
var_dump($my_var);return?$my_varcontains input from a form (as your first sentence suggests), it will contain data of type string - php doesn't do any automatic conversion of form data.