Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

filter_input_array()

Example

Utilize the filter_input_array() function to filter the three POST variables: name, age, and email.

<?php
$filters = array (
  “name” => array (“filter”=>FILTER_CALLBACK,
                             “flags”=>FILTER_FORCE_ARRAY,
                             “options”=>“ucwords”
                            ),
  “age”   => array ( “filter”=>FILTER_VALIDATE_INT,
                              “options”=>array(“min_range”=>1,“max_range”=>120)
                            ),
  “email” => FILTER_VALIDATE_EMAIL
  );
print_r(filter_input_array(INPUT_POST, $filters));
?>

The resulting output from the code above will be:

Array
  (
  [name] => Peter
  [age] => 41
  [email] => [email protected]
  )

Definition and Usage

The filter_input_array() function retrieves external variables (e.g., from form input) and optionally filters them. It is particularly useful for filtering multiple values at once, rather than repeatedly using filter_input().

Syntax

filter_input_array(type, definition, add_empty)

Parameter Values

Parameter

Description

type

Required. Specifies the input type to check, which can be one of the following:

·         INPUT_GET

·         INPUT_POST

·         INPUT_COOKIE

·         INPUT_SERVER

·         INPUT_ENV

definition

Optional. Specifies an array of filter arguments. Valid array keys are variable names, and valid values can be a filter name or ID, or an array defining the filter, flags, and options. This parameter can also be a single filter name/ID, in which case all values in the input array are filtered using that specified filter.

add_empty

Optional. A Boolean value. When set to TRUE, it adds missing keys as NULL in the return value. The default is TRUE.

Technical Details

Return Value:

An array containing the values of the variables on success, or FALSE on failure.

PHP Version:

5.2+

PHP Changelog:

PHP 5.4 introduced the add_empty parameter.