1

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....?

3
  • what does var_dump($my_var); return? Commented Jul 9, 2012 at 12:04
  • check $my_var type, if is_int($my_var) returns false, $my_var must be something else than an integer Commented Jul 9, 2012 at 12:04
  • if your variable $my_var contains 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. Commented Jul 9, 2012 at 12:05

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

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

You can reduce your whole code snippet to return (int)$int == $int;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.