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
Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…
The abs() function in PHP gives you a number without its sign — it always returns a positive value. That…
If you are working with PHP and MySQL, one of the first tasks you may need is to create a…
The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…
PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…
The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…
Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…
The whole world of PHP programming revolves around data, be it numbers, text, or more complicated structures. Each of these…
The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…
The PHP array_fill_keys function helps you to build an array where each key has the same value. What is the…