filter_input()
filter_input() — Gets a specific external variable by name and optionally filters it.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.2.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
filter_input(int $type, string $var_name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $type | int | One of the INPUT_* constants. |
| $var_name | string | Name of a variable to filter inside the corresponding type superglobal. |
| $filter | int | The filter to apply. Can be a validation filter by using one of the FILTER_VALIDATE_* constants, a sanitization filter by using one of the FILTER_SANITIZE_* or FILTER_UNSAFE_RAW, or a custom filter by using FILTER_CALLBACK. |
| $options | array|int | Either an associative array of options, or a bitmask of filter flag constants FILTER_FLAG_*. |
Return value
Value of the requested variable on success, false if the filter fails, or null if the var_name variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns false if the variable is not set and null if the filter fails.
Example
A runnable snippet:
$search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS); $search_url = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_ENCODED); echo "You have searched for $search_html.\n"; echo "<a href='?search=$search_url'>Search again.</a>";
Output:
You have searched for Me & son. <a href='?search=Me%20%26%20son'>Search again.</a>
See also
filter_input_array(), filter_var(), filter_var_array()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page filter_input() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.