谁能解释一下,为什么我会收到非常奇怪的警告:
filter_input() expects parameter 1 to be long, string given
当执行代码时,那是我类(class)的一部分,看起来非常好:
public static function Input($type, $data, $filter = 'FILTER_SANITIZE_SPECIAL_CHARS')
{
$type = 'INPUT_' . $type;
return filter_input($type, $data, $filter);
}
如果我将其更改为,例如:
返回 filter_input(INPUT_POST, $data, $filter);
然后警告转到:
filter_input() 期望参数 3 很长
。
如果我使用,一切正常:
返回 filter_input(INPUT_POST, $data, FILTER_SANITIZE_SPECIAL_CHARS);
我意识到 PHP: filter_input - Manual在描述中指出:
Description
mixed filter_input ( int $type , string $variable_name [, int $filter = FILTER_DEFAULT [, mixed $options ]] )
Parameters
type
One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
问题:
- 为什么手册
filter_input ( int $type ,
- 当 INPUT_GET 和 INPUT_POST 等都不是INTEGERS. - 有没有办法使用变量将值传递给
filter_input
?
最佳答案
你应该在那里使用的是constants .这些常量具有整数值。所以文档是完全正确的,INPUT_GET
是一个整数。尝试 var_dump(INPUT_GET)
。
如果需要从字符串中获取常量值,请使用constant()
:
echo constant('INPUT_' . $type);
关于php - 使用变量将值传递给 filter_input(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995923/