The filter_input() function is used to fetch external input data and apply validation or sanitization in a single step. It is commonly used with form data, URLs, cookies, and server variables to improve application security.
Function Syntax:
// filter_input syntax structure
filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, mixed $options = null);
// Simulating GET request data
$_GET['email'] = "test@example.com";
$_GET['age'] = "25";
// Validate email input
$email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
if ($email) {
echo "Valid Email: $email
";
} else {
echo "Invalid Email
";
}
// Validate integer input
$age = filter_input(INPUT_GET, 'age', FILTER_VALIDATE_INT);
if ($age) {
echo "Valid Age: $age
";
} else {
echo "Invalid Age
";
}
You can replace INPUT_GET with INPUT_POST and test the behavior using an HTML form submission.