filter_var()
filter_var() — Filters a variable with a specified filter.
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.
What it does
Filter a variable using a FILTER_VALIDATE_* validation filters, a FILTER_SANITIZE_* sanitization filters, or a custom filter.
Signature
filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $value | mixed | Value to filter. |
| $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
On success returns the filtered data. On failure false is returned, unless the FILTER_NULL_ON_FAILURE flag is used, in which case null is returned.
Example
A runnable snippet:
var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)); var_dump(filter_var('https://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
Output:
string(15) "bob@example.com" bool(false)
See also
filter_var_array(), filter_input(), filter_input_array()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page filter_var() (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.