0

I've used either function for checking and validating integers in many applications like so

$foo = $bar;

if (filter_var($foo,FILTER_VALIDATE_INT)) {
// do something
}

AND

if (is_int($foo)) {
 // do something
}

Both works perfectly but I want to know the difference between the two in terms of speed and results because PHP being a tricky language has differences between functions that seem to do the same thing e.g mt_rand and rand

Maverick
  • 962
  • 9
  • 12

1 Answers1

6

The filter functions are designed to work on user input, which is always a string. FILTER_VALIDATE_INT will validate whether a string is a valid integer (or it's already an int) and return an int if so.

is_int just tells you whether the type of a value is int.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • It's also easy to mix up what `is_int()` and `is_numeric()` do. `filter_var($, FILTER_VALIDATE_INT)` behaves more like `is_numeric($v) ? (int)$v : false`. – Cobra_Fast Jan 16 '19 at 15:58