diff --git a/reference/filter/functions/filter-input-array.xml b/reference/filter/functions/filter-input-array.xml index b7d5f64aecf3..48edf7a795c4 100644 --- a/reference/filter/functions/filter-input-array.xml +++ b/reference/filter/functions/filter-input-array.xml @@ -52,24 +52,136 @@ On failure, &false; is returned. - Except if the failure is that the input array designated by - type is not populated where &null; is returned - if the FILTER_NULL_ON_FAILURE flag is used. + If the input array designated by type is not + populated, &null; is returned instead. - Missing entries from the input array will be populated into the returned - &array; if add_empty is &true;. - In which case, missing entries will be set to &null;, - unless the FILTER_NULL_ON_FAILURE flag is used, - in which case it will be &false;. + Missing entries from the input array are added to the returned &array; as + &null; if add_empty is &true;, + and are omitted entirely if it is &false;. + Unlike filter_input, + the FILTER_NULL_ON_FAILURE flag does not change this: + a missing entry is always &null;. An entry of the returned &array; will be &false; if the filter fails, unless the FILTER_NULL_ON_FAILURE flag is used, in which case it will be &null;. + With the FILTER_FORCE_ARRAY flag, + that failure value is wrapped in a one element &array; + like any other result. + + &reftitle.examples; + + A <function>filter_input_array</function> example + + This example assumes a GET request to + ?email=user@example.com&age=twenty&url=https://example.com. + The age entry fails because twenty is + not an integer; a value outside the + 1 to 120 range would fail the same way. + + + FILTER_VALIDATE_EMAIL, + 'age' => [ + 'filter' => FILTER_VALIDATE_INT, + 'options' => ['min_range' => 1, 'max_range' => 120], + ], + 'url' => FILTER_VALIDATE_URL, +]; + +$result = filter_input_array(INPUT_GET, $filters); + +var_dump($result); +?> +]]> + + &example.outputs.similar; + + + string(16) "user@example.com" + ["age"]=> + bool(false) + ["url"]=> + string(19) "https://example.com" +} +]]> + + + + Filtering POST data with <function>filter_input_array</function> + + This example assumes a POST request with fields + username=<script>alert</script> and + comment=Hello World. + No missing field is submitted: because + add_empty defaults to &true;, it is still present in + the result, set to &null;. + + + FILTER_SANITIZE_SPECIAL_CHARS, + 'comment' => FILTER_SANITIZE_SPECIAL_CHARS, + 'missing' => FILTER_VALIDATE_INT, +]; + +$result = filter_input_array(INPUT_POST, $filters); + +var_dump($result); +?> +]]> + + &example.outputs.similar; + + + string(38) "<script>alert</script>" + ["comment"]=> + string(11) "Hello World" + ["missing"]=> + NULL +} +]]> + + + + Requesting an input type that is not populated + + This example assumes a GET request. + Because the request carried no POST fields, + the input array designated by INPUT_POST is not + populated and &null; is returned instead of an &array;. + This applies to every input type: + a request with no query string yields &null; for + INPUT_GET as well. + + + FILTER_VALIDATE_INT])); +?> +]]> + + &example.outputs.similar; + + + + + + &reftitle.notes; diff --git a/reference/filter/functions/filter-var-array.xml b/reference/filter/functions/filter-var-array.xml index 8e866de0f5de..5e99d93a2460 100644 --- a/reference/filter/functions/filter-var-array.xml +++ b/reference/filter/functions/filter-var-array.xml @@ -48,7 +48,7 @@ The option array is an associative array where the key corresponds - to a key in the data array and the associated + to a key in the input array and the associated value is either the filter to apply to this entry, or an associative array describing how and which filter should be applied to this entry. @@ -62,7 +62,7 @@ FILTER_UNSAFE_RAW, or FILTER_CALLBACK constants. It can optionally contain the 'flags' key - which specifies and flags that apply to the filter, + which specifies any flags that apply to the filter, and the 'options' key which specifies any options that apply to the filter. @@ -81,17 +81,39 @@ &reftitle.returnvalues; - - An array containing the values of the requested variables on success, or &false; - on failure. An array value will be &false; if the filter fails, or &null; if - the variable is not set. - + + On success, an &array; containing the values of the requested variables. + + + On failure, &false; is returned. + + + Missing entries from the input array are added to the returned &array; as + &null; if add_empty is &true;, + and are omitted entirely if it is &false;. + + + An entry of the returned &array; will be &false; if the filter fails, + unless the FILTER_NULL_ON_FAILURE flag is used, + in which case it will be &null;. + With the FILTER_FORCE_ARRAY flag, + that failure value is wrapped in a one element &array; + like any other result. + &reftitle.examples; A <function>filter_var_array</function> example + + Entries are filtered as scalars unless + FILTER_REQUIRE_ARRAY or + FILTER_FORCE_ARRAY is used. + The FILTER_REQUIRE_SCALAR flag on + testscalar below therefore only states that default + explicitly. + NULL } +]]> + + + + Applying a single filter to all values + + When options is an &integer;, the same filter + is applied to every entry in the array. + + + 'John', + 'email' => 'john@example