filter_id()
Learn how PHP's filter_id() function converts a filter name into its integer filter ID, with examples, return values, and common gotchas.
Introduction
filter_id() is a small but handy PHP function that does one job: it takes a filter name (a string such as "validate_email") and returns that filter's numeric ID — the integer that the constants FILTER_VALIDATE_EMAIL, FILTER_SANITIZE_STRING, and friends actually hold under the hood.
It is essentially the inverse of filter_list(), which gives you the list of filter names. You reach for filter_id() when you have a filter's name as a string — typically from configuration, a database, or user input — and you need the integer that filter_var() and filter_input() expect.
This page covers the syntax, return values, a runnable example, and the most common mistake people make with it.
Syntax
filter_id(string $name): int|falseIt takes a single parameter:
$name— the name of the filter as a string, e.g."validate_int","validate_email","sanitize_email". These are the lowercase names returned byfilter_list(), not theFILTER_*constant names.
It returns the integer ID of the matching filter, or false if no filter has that name.
Pass the filter name, not the constant name
This is the part that trips people up. The argument is the short, lowercase filter name — not the FILTER_VALIDATE_EMAIL constant and not the string "FILTER_VALIDATE_EMAIL".
<?php
var_dump(filter_id('validate_email')); // int(274) ✅ correct name
var_dump(filter_id('FILTER_VALIDATE_EMAIL')); // bool(false) ❌ that is the constant, not the name
var_dump(filter_id('not_a_real_filter')); // bool(false) ❌ unknown nameBecause an unknown name returns false, always check the result before using it as a filter ID. A false would otherwise be silently coerced to 0 if passed where an integer is expected.
A practical example
A realistic use case: you let users pick a validation rule by name (from a form, config file, or API request) and need to apply it dynamically. filter_id() turns that name into the ID filter_var() needs.
<?php
$ruleName = 'validate_email'; // could come from user input or config
$value = '[email protected]';
$filterId = filter_id($ruleName);
if ($filterId === false) {
echo "Unknown filter: $ruleName";
} elseif (filter_var($value, $filterId) !== false) {
echo "'$value' passed the '$ruleName' filter (id $filterId).";
} else {
echo "'$value' failed the '$ruleName' filter.";
}
// Output: '[email protected]' passed the 'validate_email' filter (id 274).Note the strict === false check: a valid filter such as int has ID 257, which is truthy, but relying on loose truthiness would still mishandle an unknown filter. Comparing against false explicitly keeps the two cases distinct.
Discovering valid filter names
To see exactly which names filter_id() accepts, iterate over filter_list():
<?php
foreach (filter_list() as $name) {
echo $name . ' => ' . filter_id($name) . PHP_EOL;
}
// int => 257
// boolean => 258
// validate_email => 274
// validate_url => 273
// sanitize... and so onWhen to use it
- Dynamic validation — when the filter to apply is decided at runtime from a string.
- Mapping config to filters — turning human-readable rule names in a config file into the IDs
filter_var()/filter_input()require. - Introspection / tooling — listing available filters and their IDs.
If you already know the filter at write time, skip filter_id() and use the constant directly (filter_var($email, FILTER_VALIDATE_EMAIL)) — it is clearer and avoids a lookup.
Related functions
filter_list()— get all supported filter names.filter_var()— filter a single variable by filter ID.filter_input()— filter a value coming from an input source.php-filters— overview of PHP's filter extension.