Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

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): mixed

Parameters

ParameterTypeDescription
$typeintThe source: `INPUT_GET`, `INPUT_POST`, `INPUT_COOKIE`.
$var_namestringThe parameter name.
$filterintOptional. The filter, such as `FILTER_VALIDATE_INT`.
$optionsarray|intOptional. 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.

Thanks for reading!

Donate to the site — we work hard for you.