1

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

albator
  • 859
  • 1
  • 10
  • 18

1 Answers1

2

$_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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • thanks, so if i really need to test is_int , i need to do $bob = (int) $_POST['foo']; THEN if(is_int($bob)) , thats right ?! – albator Dec 06 '13 at 15:53
  • @albatros that would not make sense: `(int)` will always create an integer. The problem is that it will convert any non-integer to 0. Do as PKeidel says above, test whether it is numeric first, then convert it into an int. – Pekka Dec 06 '13 at 15:54