php - 使用变量将值传递给 filter_input()

标签 php filter-input

谁能解释一下,为什么我会收到非常奇怪的警告:

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.


问题:

  1. 为什么手册 filter_input ( int $type , - 当 INPUT_GETINPUT_POST 等都不是INTEGERS.
  2. 有没有办法使用变量将值传递给 filter_input

最佳答案

你应该在那里使用的是constants .这些常量具有整数值。所以文档是完全正确的,INPUT_GET 是一个整数。尝试 var_dump(INPUT_GET)

如果需要从字符串中获取常量值,请使用constant():

echo constant('INPUT_' . $type);

关于php - 使用变量将值传递给 filter_input(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995923/

相关文章:

php - 表tr背景颜色变化

php - 如何向 filter_input 添加选项和标志

c# - NEST Elasticsearch :FilterInputs术语和术语之间的区别?

javascript - 目标ajax请求后filter_input和直接访问$_POST之间的区别

php - 堆栈与排队?

php - 在数据库中注册生成的 uniqid

php - 通过mysql查询检索PHP中的 'œ'字符

php - filter_var 和 filter_input 在输入数据验证方面的区别

php - 通过 PHP 广播推送通知