PHP Ferrari
PHP Ferrari

Reputation: 15616

PHP: is_int return wrong result

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

Upvotes: 1

Views: 3476

Answers (2)

Frankline
Frankline

Reputation: 40985

// 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"];

Upvotes: 2

Floern
Floern

Reputation: 33904

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.

Upvotes: 8

Related Questions