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 = "user@example.com";
$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%20AlbertThe %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?
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()?
$email = "user@example.com";
$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";
}
Similar Reads
PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…
Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…
Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…
The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…
The PHP “while” loop is a simple tool that repeatedly runs a block of code as long as a certain…
One of the important tasks is retrieving documents from a collection. MongoDB help us to make this process, but understanding…
PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…
PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…
Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…
If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…