PHP filter_id() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filter

Definition

In PHP, filter_id() turns the name of a filter, such as 'validate_email', into its numeric ID, for example the value of FILTER_VALIDATE_EMAIL.

Every filter in PHP has a short text name and a number. filter_id() lets you look up the number from the name, which is handy when the filter name comes from a config file or a database.

Syntax

Call filter_id() with the values you need. Start with this line.

PHP
$id = filter_id('validate_email');
CallWhat it does
filter_id('validate_email')Returns the ID of the email validation filter, the same value as FILTER_VALIDATE_EMAIL.
filter_id('int')Returns the ID of the integer validation filter, the same value as FILTER_VALIDATE_INT.
filter_id('unknown_filter')Returns false because no filter has that name.

Use filter_list() to see every valid name. The name for FILTER_VALIDATE_INT is 'int', and the name for FILTER_VALIDATE_EMAIL is 'validate_email'. Always compare the result with === false to detect an unknown name.

Library declarations
PHP
filter_id(string $name): int|false

Parameters

ParameterDescription
nameRequired. The name of a filter as returned by filter_list(), for example 'validate_email' or 'int'.

Return Value

Return ValueDescription
intThe numeric ID of the filter, which you can pass to filter_var() or filter_input().
falseReturned when no filter with that name exists.

PHP Examples

Example 1 — Look Up the Email Filter ID

Get the ID for the 'validate_email' filter and confirm it matches the FILTER_VALIDATE_EMAIL constant.

PHP
<?php
$id = filter_id('validate_email');

echo "Matches constant: ", $id === FILTER_VALIDATE_EMAIL ? "yes" : "no", PHP_EOL;

How It Works

  1. filter_id('validate_email') looks up the number for that filter name.
  2. The result is stored in $id.
  3. FILTER_VALIDATE_EMAIL holds the very same number.
  4. The strict comparison with === prints yes.

Example 2 — Unknown Names Return false

When a name does not exist, filter_id() gives back false, so always check with ===.

PHP
<?php
$known = filter_id('int');
$unknown = filter_id('validate_int');

echo "int: ", $known === FILTER_VALIDATE_INT ? "found" : "missing", PHP_EOL;
echo "validate_int: ", $unknown === false ? "not a filter name" : "found", PHP_EOL;

How It Works

  1. The filter for whole numbers is named int, so filter_id('int') returns its ID.
  2. validate_int is only a guess. No filter uses that name.
  3. filter_id() returns false for the unknown name.
  4. Checking with === false avoids mixing it up with a real ID.

Example 3 — Choose a Filter by Name

Read a filter name from a variable, convert it with filter_id(), and use it with filter_var().

PHP
<?php
$filterName = 'validate_email';
$filterId = filter_id($filterName);

if ($filterId === false) {
    echo "Unknown filter.", PHP_EOL;
    exit;
}

$result = filter_var('[email protected]', $filterId);
echo $result !== false ? "Valid: $result" : "Invalid value", PHP_EOL;

How It Works

  1. The filter name is kept in a variable, like it might be in a config file.
  2. filter_id() converts that name into the numeric ID.
  3. If the ID is false, the script stops with a message.
  4. Otherwise the ID is passed to filter_var(), which validates the email and returns it.

Frequently Asked Questions

It converts a filter name, like 'validate_email', into the numeric filter ID that functions such as filter_var() accept. It returns false if the name is not known.
Call filter_list(). It returns an array of every filter name supported by your PHP build, for example int, validate_email, and unsafe_raw.
The name is 'int'. So filter_id('int') returns the same number as the constant FILTER_VALIDATE_INT. Similarly, 'validate_email' matches FILTER_VALIDATE_EMAIL.
Use the constants whenever you know the filter ahead of time. They are easier to read. Use filter_id() only when the filter name is stored in text, like a config file.
It returns false. Compare with === false so you can tell it apart from a real ID.

Next: filter_input()

filter_input() gets a single variable from GET, POST, cookies, or the server, and can vali.

filter_input() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful