Signature
filter_id(string $name): int|falsePurpose
The filter extension identifies each filter with an integer, and the familiar constants such as FILTER_VALIDATE_INT or FILTER_VALIDATE_EMAIL are names for those integers. filter_id() goes the other way around: you give it the textual name of a filter and it hands back the matching numeric ID. This is useful when the filter to apply is stored as text, for example in a configuration array or a form-schema definition, and you need a value that filter_var() accepts.
The names it understands are the ones reported by filter_list(), such as int, boolean, float, validate_email, validate_url or unsafe_raw. Note that these are the short filter names, not the constant names. FILTER_VALIDATE_INT is a constant; int is the name.
Signature
Parameters and defaults
$name: the name of the filter you want to look up. It is required and has no default. Pass the name exactly asfilter_list()reports it. Do not pass theFILTER_constant name as a string.
Return value and failure cases
On success the function returns the integer ID of the filter. If no filter with that name exists, it returns false. It does not throw an exception for an unknown name, so the caller is responsible for checking the result.
Because an ID is an integer and failure is false, always test with a strict comparison such as === false. Do not rely on a loose truthiness check as your only guard.
Example 1: names and constants agree
The ID you get from a name is the same integer that the matching constant holds. Comparing against the constants avoids hard-coding numbers in your own code.
$checks = [
'int' => filter_id('int') === FILTER_VALIDATE_INT,
'validate_email' => filter_id('validate_email') === FILTER_VALIDATE_EMAIL,
'unsafe_raw' => filter_id('unsafe_raw') === FILTER_UNSAFE_RAW,
];
echo json_encode($checks), PHP_EOL;{"int":true,"validate_email":true,"unsafe_raw":true}
Example 2: unknown names and the list of filters
An unknown name produces false. Every name that filter_list() reports should resolve to an ID, so collecting the names that fail gives an empty list.
var_dump(filter_id('no_such_filter'));
$missing = array_filter(
filter_list(),
fn(string $name): bool => filter_id($name) === false
);
echo json_encode(array_values($missing)), PHP_EOL;bool(false) []
Example 3: using the ID with filter_var()
When the filter name comes from data, convert it once and pass the result as the filter argument of filter_var().
$id = filter_id('int');
var_dump(filter_var('42', $id));
var_dump(filter_var('4x', $id));int(42) bool(false)
The valid string becomes the integer 42, while the invalid one makes filter_var() return false, which is that function's normal failure result for a validation filter.
Edge cases
- Unknown or misspelled names return
false. This includes names that are plausible but not listed, so a typo in a configuration file will silently producefalseunless you check it. - Constant names as strings such as
'FILTER_VALIDATE_INT'are not filter names. Use'int'instead, or use the constant directly if the filter is fixed in your code. - Aliases exist in the list, for example both
booleanandboolare reported byfilter_list(). Each name is looked up by its own entry. - Empty string is not a filter name, so treat it like any other unknown name and expect
false. - Availability is determined by the filter extension at runtime, so
filter_list()is the authority on what your build supports.
Common mistakes
- Not checking for false. Passing the result straight into
filter_var()when the name is unknown handsfalseto the filter argument, which does not do what you intended. Validate the lookup first. - Using the constant name.
filter_id('FILTER_VALIDATE_EMAIL')is not the right lookup; the name isvalidate_email. - Hard-coding the integer. Writing
257in your source makes the code hard to read. Prefer the constant, or callfilter_id()with a name. - Confusing the ID with a flag. The result identifies a filter. Flags and options such as
FILTER_NULL_ON_FAILUREare passed separately in the third argument offilter_var(). - Expecting it to validate data.
filter_id()only looks up a filter. It never inspects user input.
Related functions
filter_list()returns the names of all supported filters, which are the valid input forfilter_id().filter_var()applies a filter, identified by its ID or constant, to a single value.filter_var_array()andfilter_input()apply filters to several values or to request input.
Recap
filter_id() translates a filter name into its integer ID and returns false when the name is unknown. Use names from filter_list(), compare the result strictly against false, and then pass the ID to filter_var().
Tested examples
$checks = [
'int' => filter_id('int') === FILTER_VALIDATE_INT,
'validate_email' => filter_id('validate_email') === FILTER_VALIDATE_EMAIL,
'unsafe_raw' => filter_id('unsafe_raw') === FILTER_UNSAFE_RAW,
];
echo json_encode($checks), PHP_EOL;{"int":true,"validate_email":true,"unsafe_raw":true}
var_dump(filter_id('no_such_filter'));
$missing = array_filter(
filter_list(),
fn(string $name): bool => filter_id($name) === false
);
echo json_encode(array_values($missing)), PHP_EOL;bool(false) []
$id = filter_id('int');
var_dump(filter_var('42', $id));
var_dump(filter_var('4x', $id));int(42) bool(false)