Validation7 min readExamples run on PHP 8.5.10

filter_id()

Learn how filter_id() converts a filter name such as int into its numeric ID, when it returns false, and how to use the ID with filter_var().

Signature

filter_id(string $name): int|false
Available since PHP 5.2.07.48.08.18.28.38.48.5All versions →

Purpose

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 as filter_list() reports it. Do not pass the FILTER_ 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.

Example
<?php
$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;
Output
{"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.

Example
<?php
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;
Output
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().

Example
<?php
$id = filter_id('int');
var_dump(filter_var('42', $id));
var_dump(filter_var('4x', $id));
Output
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 produce false unless 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 boolean and bool are reported by filter_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

  1. Not checking for false. Passing the result straight into filter_var() when the name is unknown hands false to the filter argument, which does not do what you intended. Validate the lookup first.
  2. Using the constant name. filter_id('FILTER_VALIDATE_EMAIL') is not the right lookup; the name is validate_email.
  3. Hard-coding the integer. Writing 257 in your source makes the code hard to read. Prefer the constant, or call filter_id() with a name.
  4. Confusing the ID with a flag. The result identifies a filter. Flags and options such as FILTER_NULL_ON_FAILURE are passed separately in the third argument of filter_var().
  5. Expecting it to validate data. filter_id() only looks up a filter. It never inspects user input.
  • filter_list() returns the names of all supported filters, which are the valid input for filter_id().
  • filter_var() applies a filter, identified by its ID or constant, to a single value.
  • filter_var_array() and filter_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

Names and constants agree
<?php
$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;
Output
{"int":true,"validate_email":true,"unsafe_raw":true}
Unknown names and the list of filters
<?php
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;
Output
bool(false)
[]
Using the ID with filter_var()
<?php
$id = filter_id('int');
var_dump(filter_var('42', $id));
var_dump(filter_var('4x', $id));
Output
int(42)
bool(false)