PHP filter_id Function: How to Retrieve PHP Filter IDs

filter_id in PHP

Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP 5.2 added a filter system, and filter_id() was introduced to map filter names to their numeric IDs.

In this article, you will learn how to get filter IDs by examples. Let’s get started.

What is filter_id()?

filter_id() returns the filter ID for a given filter name. It helps when you use PHP’s filter system. It converts a filter’s name like “email” into a numeric ID such as 517.

Here is its syntax:

filter_id(string $filter_name)
  • $filter_name: A string like “int”, “email”, or “string”.
  • Returns: The numeric ID as an integer.
  • Returns false if the name is not valid.

Here is a quick example:

echo filter_id("email"); 

The output:

517

This helps when you store or compare filters as IDs.

So, why use it?

  • Use filter_id() when your code works with filters stored in variables. It lets you pass numeric IDs to filtering functions instead of hardcoding names.
  • PHP assigns specific numeric values to filters and filter_id() helps retrieve them.

Let’s move on to the following section to understand how to use filter_id() with filter_var() and filter_input().

Use filter_id() with filter_var() and filter_input()

The filter_var() function filters a variable with a specific filter. Instead of using a filter name, you can pass its ID with filter_id().

Example:

$email = "[email protected]";
$filter = filter_id("email");  

if (filter_var($email, $filter)) {
    echo "Valid email";
} else {
    echo "Invalid email";
}

Output:

Valid email

Here:

  • You won’t need to update your code manually if the filter name changes in future PHP versions.
  • Helps when filter names are stored in variables.

filter_input() retrieves and filters external variables (e.g., from $_GET, $_POST). You can use filter_id() to dynamically specify the filter.

Here is an example:

First, you need to pass the name variable as a param to the URL like this:

localhost/script.php?name=Jasmeen%20Albert

The %20 represents the space between the words in a URL.

Here is the code in script.php page

$filter = filter_id("string"); 
$name = filter_input(INPUT_GET, "name", $filter);

if ($name !== null && $name !== false) {
    echo "Sanitized name: " . $name;
} else {
    echo "Invalid input";
}

This will show you the following output:

Jasmeen Albert
  • It helps when your code uses different filters based on user input or settings.
  • Useful when filters are stored in variables and need to be applied conditionally.

Wrapping Up

In this article, you learned how to use filter_id() with filter_var() and filter_input() to dynamically validate and sanitize data in PHP.

Here is a quick recap:

  • filter_id() converts a filter name into its numeric ID.
  • It makes filtering more dynamic and adaptable.
  • Works well with filter_var() for validating and sanitizing variables.
  • Helps filter_input() fetch and sanitize user input dynamically.

What is filter_id() in PHP?

filter_id() is a PHP function that returns the numeric ID of a given filter name. It simplifies working with PHP’s filter system by converting a filter’s string name into its corresponding ID.
echo filter_id("email");  
// Output: 517

Why use filter_id() instead of filter names?

  • Makes handling user input more dynamic.
  • Helps retrieve filter IDs assigned by PHP.
  • Useful when storing filter types in a database, as IDs are more efficient than names.

How do you use filter_id() with filter_var()?

You can use filter_id() to dynamically retrieve a filter ID and pass it to filter_var() for validation. For example
$email = "[email protected]";
$filter = filter_id("email");

if (filter_var($email, $filter)) {
    echo "Valid email";
} else {
    echo "Invalid email";
}
filter_input() retrieves and filters external input. Using filter_id(), you can dynamically specify the filter.
$filter = filter_id("string"); 
$name = filter_input(INPUT_GET, "name", $filter);

if ($name !== null && $name !== false) {
    echo "Sanitized name: " . $name;
} else {
    echo "Invalid input";
}
Previous Article

PHP filter_input_array: How to Clean and Validate Input

Next Article

PHP filter_var: Understand How to Sanitize Input

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.