Pass a value into filter_input() using variable

前端 未结 3 1191
一向
一向 2021-01-21 23:24

Can anyone please explain, why do I get very strange warning:

filter_input() expects parameter 1 to be long, string given

when executing the co

相关标签:
3条回答
  • 2021-01-22 00:05

    INPUT_POST and INPUT_GET are defined as follows:

    /**
     * POST variables.
     * @link http://www.php.net/manual/en/filter.constants.php
     */
    define ('INPUT_POST', 0);
    
    /**
     * GET variables.
     * @link http://www.php.net/manual/en/filter.constants.php
     */
    define ('INPUT_GET', 1);
    
    0 讨论(0)
  • 2021-01-22 00:12

    Here the problem. When you concatenate 'INPUT_' with variable it bacame a string, see example:

    echo $type = 'INPUT_' . 'POST'; // give you a string INPUT_POST
    
    echo INPUT_POST; //give you 0
    

    That's why :

    filter_input() expects parameter 1 to be long, string given
    
    0 讨论(0)
  • 2021-01-22 00:23

    What you're supposed to use there are constants. These constants have integer values. So the documentation is entirely correct, INPUT_GET is an integer. Try var_dump(INPUT_GET).

    If you need to get a constant value from a string, use constant():

    echo constant('INPUT_' . $type);
    
    0 讨论(0)
提交回复
热议问题