Search code examples
phppostisnumeric

Test is_int doen't work after passing $_POST var in simple $var


Is someone can explain me this..:

$bob = $_POST['foo'] ;

is_int($bob) FAIL but is_numeric($bob) is OK .

So i know i cannot use is_int directly on $_POST but here i transfert the post value in another variable before..

Whats wrong please ?!


Solution

  • $_POST values are strings, no matter whether they contain a numeric value or not. Just transferring them to a different variable doesn't change that.

    You'd have to typecast the variable:

    $bob = (int) $_POST['foo'];
    

    but note that non-integer values are cast to 0 in this case.