filter_var() and filter_input()

Can someone please explain the difference between filter_var() and filter_input(), besides that you can’t use filter_input() unless the variable was inputted?

Is there anything wrong with the following?


function SanitizeInt($field)
{
	$field=filter_var($field,FILTER_SANITIZE_NUMBER_INT);
	return $field;
}
$year=SanitizeInt($_POST['year']);

If not, what’s the use of filter_input()?

That’s exactly what [fphp]filter_input[/fphp] already does.

$year = filter_input(INPUT_POST, 'year', FILTER_SANITIZE_NUMBER_INT);

I don’t understand what you mean. My question is: why would I ever use filter_input when I can just use filter_var instead?

The function I made is just a shorthand since there are so many variables to filter.

filter_input() won’t throw a notice error if the array indice doesn’t exist. This may or may not save you a step of using isset()

Hmm. I’ve never gotten an error, even when the field is left blank. Is it possible the function is suppressing the error since it’s not being returned?

error_reporting must be set to a level high enough to include errors of type E_NOTICE, if you want php to report them.