filter_input
Reads a value from the request and validates it at once.
Syntax
filter_input(int $type, string $var_name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixedParameters
| Parameter | Type | Description |
|---|---|---|
| $type | int | The source: `INPUT_GET`, `INPUT_POST`, `INPUT_COOKIE`. |
| $var_name | string | The parameter name. |
| $filter | int | Optional. The filter, such as `FILTER_VALIDATE_INT`. |
| $options | array|int | Optional. Filter options. |
Return value
mixedThe value, `false` if validation fails, or `null` if the parameter is absent.
Examples
A page number from the address:
<?php
// search.php
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, [
'options' => ['default' => 1, 'min_range' => 1],
]);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);Notes
Three different results: the value, false (failed validation) and null (no such parameter). Compare with ===.
See also
- filter_var — Validates or sanitises a value by a given rule.
- htmlspecialchars — Replaces HTML special characters with safe entities.
- intval — Converts a value to an integer.
Related recipes
In the tutorial
- Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.