When writing PHP applications, you may need to validate multiple external input fields using a rules array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The filter_input_array() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is filter_input_array() in PHP?
Validate multiple external input fields using a rules array.
Its syntax is:
filter_input_array(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null
Parameters used by this operation:
$type— The value supplied for this argument affects the operation.$options— The value supplied for this argument affects the operation.$add_empty— The value supplied for this argument affects the operation.
Reads the original external input provided to PHP rather than mutations to $_GET or $_POST. Missing and invalid values require separate handling; results depend on actual request input.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$rules = ["page" => FILTER_VALIDATE_INT, "email" => FILTER_VALIDATE_EMAIL];
$values = filter_input_array(INPUT_GET, $rules);
var_export($values);
Output: Depends on the request environment, installed extension, selected locale, or filesystem state.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$rules = ["age" => ["filter" => FILTER_VALIDATE_INT, "options" => ["min_range" => 1]]];
$values = filter_input_array(INPUT_POST, $rules);
var_export($values);
Output: Depends on the request environment, installed extension, selected locale, or filesystem state.
Practical Example: Validate query parameters on the server before using them to select records
Validate query parameters on the server before using them to select records. Save the code in a PHP file and adapt the sample data to your project.
<?php
$rules = ["page" => FILTER_VALIDATE_INT, "email" => FILTER_VALIDATE_EMAIL];
$validatedInput = filter_input_array(INPUT_GET, $rules);
if ($validatedInput === null) { echo "No query input available"; } else { var_export($validatedInput); }
Output: Depends on the request environment, installed extension, selected locale, or filesystem state.
Important Behavior and Edge Cases
Reads the original external input provided to PHP rather than mutations to $_GET or $_POST. Missing and invalid values require separate handling; results depend on actual request input.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
filter_input_array() | Validate multiple external input fields using a rules array |
| Related functions | filter_var_array() validates a supplied array rather than request input. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating a missing value as valid | Check absent fields separately from fields that fail validation. |
| Assuming validation authorizes a change | Validation checks data format; permissions require separate enforcement. |
| Checking only the top-level result | Reads the original external input provided to PHP rather than mutations to $_GET or $_POST. Missing and invalid values require separate handling; results depend on actual request input. |
Key Takeaways
filter_input_array()helps you validate multiple external input fields using a rules array.- Reads the original external input provided to PHP rather than mutations to $_GET or $_POST. Missing and invalid values require separate handling; results depend on actual request input.
- filter_var_array() validates a supplied array rather than request input.
- Use the practical example as a starting point, but validate real application inputs.
filter_input_array() is useful when you need to validate multiple external input fields using a rules array. Use its documented rules and the examples above to choose the right behavior for your data.