In PHP, filter_input() gets a single variable from GET, POST, cookies, or the server, and can validate or clean it in the same step.
Instead of reading $_GET['email'] directly and hoping it is safe, you ask filter_input() for the value with a filter such as FILTER_VALIDATE_EMAIL. It returns the cleaned value, false if it is invalid, or null if the variable is missing.
Syntax
Call filter_input() with the values you need. Start with this line.
With the flag, a missing variable returns false and a failed filter returns null.
The first argument is one of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. The default filter FILTER_DEFAULT (same as FILTER_UNSAFE_RAW) does not change the value, so it is not a safety filter on its own.
Required. The input source: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
var_name
Required. The name of the variable to read, for example 'email'.
filter
Optional. The filter ID to apply, such as FILTER_VALIDATE_EMAIL. The default is FILTER_DEFAULT.
options
Optional. An array with 'options' and 'flags' keys, or a single flags value such as FILTER_NULL_ON_FAILURE. The default is 0.
Return Value
Return Value
Description
mixed
The filtered value on success (a string, int, float, or bool depending on the filter).
false
Returned when the filter fails (for example an invalid email).
null
Returned when the variable is not set.
PHP Examples
Example 1 — Read and Validate an Email
Read the email field from the query string and handle each possible result. From the command line there is no query string, so the variable is missing.
PHP
<?php
$email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
if ($email === null) {
echo "No email was sent.", PHP_EOL;
} elseif ($email === false) {
echo "That email is not valid.", PHP_EOL;
} else {
echo "Valid email: $email", PHP_EOL;
}
Output
No email was sent.
How It Works
filter_input() looks for email in the GET data of the request.
If email is missing, the result is null and the first branch runs.
If email exists but fails FILTER_VALIDATE_EMAIL, the result is false.
A valid address is returned as a string, so the last branch would print it.
Example 2 — FILTER_NULL_ON_FAILURE Swaps the Results
With the FILTER_NULL_ON_FAILURE flag, a missing variable returns false instead of null.
The page variable is not in the GET data, so it is treated as missing.
Normally a missing variable returns null.
With FILTER_NULL_ON_FAILURE the meaning flips, so missing returns false.
var_export() prints the exact value so you can see the difference.
Example 3 — Validate a POST Age with a Range
Pass an options array to require a whole number between 18 and 65, and give a clear message for each case.
PHP
<?php
$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT, [
'options' => ['min_range' => 18, 'max_range' => 65],
]);
if ($age === null) {
echo "Age field is missing.", PHP_EOL;
} elseif ($age === false) {
echo "Age must be a number from 18 to 65.", PHP_EOL;
} else {
echo "Age accepted: $age", PHP_EOL;
}
Output
Age field is missing.
How It Works
INPUT_POST means the value must come from a submitted form body.
The options array sets min_range and max_range for FILTER_VALIDATE_INT.
Nothing was posted from the command line, so the result is null.
Inside a real form, 30 would print Age accepted: 30 and 'abc' would print the range message.
Frequently Asked Questions
It reads one variable from GET, POST, cookies, server, or environment data and applies a filter to it. You can use it to validate or sanitize user input in a single call.
filter_input() reads straight from the request source, like INPUT_GET. filter_var() filters a value you already have in a variable. Use filter_input() for incoming data and filter_var() for values from anywhere else.
null means the variable does not exist in that input source. This happens when the user did not send the field. It is different from false, which means the value failed the filter.
No. It reads the original request data, so changes you make to $_GET or $_POST later in the script are not seen. This protects you from accidentally filtering altered data.
FILTER_DEFAULT does not change the value, so it does not make input safe by itself. Use a validate filter like FILTER_VALIDATE_EMAIL, or escape output with htmlspecialchars() when printing.