PHP filter_input() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filter

Definition

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.

PHP
$email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
CallWhat it does
filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL)Returns the email if valid, false if invalid, or null if email was not sent.
filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT)Returns the age as an integer, false if it is not a whole number, or null if missing.
filter_input(INPUT_GET, 'q', FILTER_DEFAULT, FILTER_NULL_ON_FAILURE)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.

Library declarations
PHP
filter_input(int $type, string $var_name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed

Parameters

ParameterDescription
typeRequired. The input source: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
var_nameRequired. The name of the variable to read, for example 'email'.
filterOptional. The filter ID to apply, such as FILTER_VALIDATE_EMAIL. The default is FILTER_DEFAULT.
optionsOptional. 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 ValueDescription
mixedThe filtered value on success (a string, int, float, or bool depending on the filter).
falseReturned when the filter fails (for example an invalid email).
nullReturned 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;
}

How It Works

  1. filter_input() looks for email in the GET data of the request.
  2. If email is missing, the result is null and the first branch runs.
  3. If email exists but fails FILTER_VALIDATE_EMAIL, the result is false.
  4. 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.

PHP
<?php
$normal = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
$flagged = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);

echo "Without flag: ", var_export($normal, true), PHP_EOL;
echo "With flag: ", var_export($flagged, true), PHP_EOL;

How It Works

  1. The page variable is not in the GET data, so it is treated as missing.
  2. Normally a missing variable returns null.
  3. With FILTER_NULL_ON_FAILURE the meaning flips, so missing returns false.
  4. 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;
}

How It Works

  1. INPUT_POST means the value must come from a submitted form body.
  2. The options array sets min_range and max_range for FILTER_VALIDATE_INT.
  3. Nothing was posted from the command line, so the result is null.
  4. 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.

Next: filter_input_array()

filter_input_array() reads several variables from GET, POST, cookies, or the server at onc.

filter_input_array() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful