Signature
filter_list(): arrayPurpose
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.
$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";{"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.
$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";{"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.
$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";{"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
- Treating array keys as filter IDs. The key
0is not a filter constant. Callfilter_id()with the name. - Using loose
in_array()checks. Passtrueas the third argument so that only exact string matches count. - Printing the list and expecting a fixed output. Tests that depend on the entire array are brittle across builds.
- Using it for validation.
filter_list()only lists available filters. To validate a value, usefilter_var()with an ID such asFILTER_VALIDATE_INT. - Passing user input directly to
filter_id()and forgetting thefalseresult. Always check forfalsebefore using the ID.
Related functions
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()andfilter_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
$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";{"int":true,"validate_email":true,"validate_url":true,"callback":true}
$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";{"int":true,"validate_email":true,"unknown":false}
$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";{"is_list":true,"all_ids_found":true,"all_ids_are_ints":true}