Content · Web Development · PHP

PHP filter_input() Function: Usage and Examples

Reads a variable from the original request input and applies a requested filter without changing the input source.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

In PHP, filter_input() helps with runtime and application data. Reads a variable from the original request input and applies a requested filter without changing the input source.

What Is filter_input() in PHP?


The function or method has a specific purpose: Reads a variable from the original request input and applies a requested filter without changing the input source. The following examples show the main call, one additional situation, and an application-style use.

Syntax:

<?php
filter_input(int $type, string $var_name, int $filter = ..., array|int $options = ...) : mixed

Parameters and Return Value


  • $type (int) — requested type or type selector; required.
  • $var_name (string) — argument used by this API to perform its operation; required.
  • $filter (int) — filter identifier or mode; optional.
  • $options (array|int) — configuration options controlling behavior; optional.

Example 1: Basic Usage


Reads a variable from the original request input and applies a requested filter without changing the input source.

<?php
$pageNumber = filter_input(INPUT_GET, "page", FILTER_VALIDATE_INT);
var_dump($pageNumber);

This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.

Example 2: Handling a Different Case


<?php
$emailAddress = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
if ($emailAddress === false || $emailAddress === null) { echo "Email missing or invalid"; }

The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.

Practical Example


<?php
$limit = filter_input(INPUT_GET, "limit", FILTER_VALIDATE_INT, ["options" => ["min_range" => 1, "max_range" => 100]]);
$limit = $limit === false || $limit === null ? 20 : $limit;
echo $limit;

The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.

Important Behavior and Edge Cases


  • Reads a variable from the original request input and applies a requested filter without changing the input source.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available.
  • Use the documented return type and handle false, null and exceptions according to the specific call.

Comparison With Related Functions


ApproachDifference
filter_inputReads a variable from the original request input and applies a requested filter without changing the input source.
filter_varChecks a supplied PHP value rather than the original request input.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring errors from filter_inputCheck the actual result and handle the expected error path.
Using the demonstration unchanged in productionAdapt file paths, configuration, authentication and permission rules to the application.
Treating input or returned data as trustedApply validation at input boundaries and context-aware escaping at output boundaries.

Key Takeaways


  • Reads a variable from the original request input and applies a requested filter without changing the input source.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available.
  • Use the documented return type and handle false, null and exceptions according to the specific call.

Reads a variable from the original request input and applies a requested filter without changing the input source. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.

Share this resourceSend this page to someone who may find it useful.