PHP Security - (int) vs FILTER_VALIDATE_INT
The difference is that a cast to int
will always get you an int
, which may or may not be the original value. E.g. (int)'foobar'
results in the int
0
. This makes it safe for most SQL purposes, but has nothing to do with the original value, and you won't even know it.
filter_var
with FILTER_VALIDATE_INT
tells you whether the value is an int
, based on which you can make the decision to use it in an SQL query or display an error message to the user.