Validation7 min readExamples run on PHP 8.5.10

filter_list()

Learn how filter_list() returns the names of the filters available to the PHP filter extension, how it relates to filter_id(), and which mistakes to avoid.

Signature

filter_list(): array
Available since PHP 5.2.07.48.08.18.28.38.48.5All versions →

Purpose

filter_list() tells you which filters the PHP filter extension knows about. Each entry in the returned array is a filter name such as int or validate_email. These are the same names you can pass to filter_id() to get the numeric constant used by filter_var().

The function is mostly a diagnostic and discovery tool. You might use it to check that a filter exists before accepting a filter name from a configuration array, to build a lookup table of names and IDs, or to show developers which options are available in a runtime.

Signature

Parameters and defaults

filter_list() takes no parameters, so there is nothing to configure. Every call in the same runtime returns the same set of names.

Return value and failure cases

The function returns an array of strings, each one the name of a supported filter. The manual states that it returns an empty array if there are no such filters. It does not return false, and it does not throw for normal use, so you can always iterate the result with foreach.

The array keys are plain list positions. They are not filter IDs. To get an ID, pass the name to filter_id(), which returns the integer ID, or false when the name is unknown.

Example 1: check that specific filters exist

Use a strict in_array() check against the returned names. This is safer than assuming every build offers the same set.

Example
<?php
$filters = filter_list();
$wanted = ['int', 'validate_email', 'validate_url', 'callback'];
$result = [];
foreach ($wanted as $name) {
    $result[$name] = in_array($name, $filters, true);
}
echo json_encode($result), "\n";
Output
{"int":true,"validate_email":true,"validate_url":true,"callback":true}

The output is a small JSON map showing that each requested name is available.

Example 2: names versus IDs

A filter name and a filter constant refer to the same filter, but they are different values. filter_id() converts a name to the constant. An unrecognized name gives false.

Example
<?php
$checks = [
    'int' => filter_id('int') === FILTER_VALIDATE_INT,
    'validate_email' => filter_id('validate_email') === FILTER_VALIDATE_EMAIL,
    'unknown' => filter_id('not_a_filter'),
];
echo json_encode($checks), "\n";
Output
{"int":true,"validate_email":true,"unknown":false}

This confirms that the name int maps to FILTER_VALIDATE_INT and that unknown names are reported as false.

Example 3: build and verify a name-to-ID table

Because the array keys are only positions, a lookup table has to be built by calling filter_id() for every name.

Example
<?php
$names = filter_list();
$ids = [];
foreach ($names as $name) {
    $ids[$name] = filter_id($name);
}
echo json_encode([
    'is_list' => array_is_list($names),
    'all_ids_found' => !in_array(false, $ids, true),
    'all_ids_are_ints' => count(array_filter($ids, 'is_int')) === count($ids),
]), "\n";
Output
{"is_list":true,"all_ids_found":true,"all_ids_are_ints":true}

The result shows that the array is a simple list, that every listed name has an ID, and that those IDs are integers.

Edge cases

  • Do not hard-code the full list. The manual's example output is described as "something similar to" the real output. The exact names and order depend on the PHP build, so compare against the names you need rather than the whole array.
  • Several names can share one ID. Some filters have more than one accepted name, so do not assume the table of IDs is unique in both directions.
  • The result is names only. It does not describe the options, flags or behavior of each filter.
  • Empty arrays are possible in principle. The documented fallback is an empty array, so write code that handles that without errors.

Common mistakes

  1. Treating array keys as filter IDs. The key 0 is not a filter constant. Call filter_id() with the name.
  2. Using loose in_array() checks. Pass true as the third argument so that only exact string matches count.
  3. Printing the list and expecting a fixed output. Tests that depend on the entire array are brittle across builds.
  4. Using it for validation. filter_list() only lists available filters. To validate a value, use filter_var() with an ID such as FILTER_VALIDATE_INT.
  5. Passing user input directly to filter_id() and forgetting the false result. Always check for false before using the ID.
  • filter_id() converts a filter name to its numeric ID.
  • filter_var() filters or validates a single value.
  • filter_var_array() applies filters to several values at once.
  • filter_input() and filter_has_var() work with external input sources.

Recap

filter_list() has no parameters and returns an array of supported filter names, or an empty array when none exist. Its keys are not IDs; use filter_id() to get those. Check for the specific names you need with strict comparisons, and avoid depending on the exact order or full contents of the list.

Tested examples

Check that specific filters exist
<?php
$filters = filter_list();
$wanted = ['int', 'validate_email', 'validate_url', 'callback'];
$result = [];
foreach ($wanted as $name) {
    $result[$name] = in_array($name, $filters, true);
}
echo json_encode($result), "\n";
Output
{"int":true,"validate_email":true,"validate_url":true,"callback":true}
Names versus IDs with filter_id()
<?php
$checks = [
    'int' => filter_id('int') === FILTER_VALIDATE_INT,
    'validate_email' => filter_id('validate_email') === FILTER_VALIDATE_EMAIL,
    'unknown' => filter_id('not_a_filter'),
];
echo json_encode($checks), "\n";
Output
{"int":true,"validate_email":true,"unknown":false}
Build and verify a name-to-ID table
<?php
$names = filter_list();
$ids = [];
foreach ($names as $name) {
    $ids[$name] = filter_id($name);
}
echo json_encode([
    'is_list' => array_is_list($names),
    'all_ids_found' => !in_array(false, $ids, true),
    'all_ids_are_ints' => count(array_filter($ids, 'is_int')) === count($ids),
]), "\n";
Output
{"is_list":true,"all_ids_found":true,"all_ids_are_ints":true}