Why does filter_input () return NULL when I manually assign a value to the $ _POST array?

I follow some data verification exercises and decided to team up with the function filter_input(). When I try to run this code:

$_POST['var'] = 10;

$filtered = filter_input(INPUT_POST, 'var', FILTER_VALIDATE_FLOAT);

var_dump($filtered);

var_dump($filtered)returns null. I know that code works if $_POST['var']a value is assigned by submitting a form, but I'm just wondering why manually assigning a value to an array $_POST[]does not return float(10)?

+3
source share
3 answers

filter_input()cannot be read from supergroup arrays _POST / _GET / _COOKIE / _SERVER / _ENV. He reads from the source of these values, respectively (which are also used to fill the superglobal).

+10
source

, INPUT_POST POST, script . , var, :

filter_var($_POST['var'], FILTER_VALIDATE_FLOAT);
+4

If you want to set the input value and the default filter, use filter_var. Since filter_input does not filter the current values ​​of superglobals.

+1
source

All Articles