如何动态设置参数为$ _GET或$ _POST?

时间:2017-12-05 01:53:03

标签: php

如果设置了所有参数,我正在php中编写一个函数来检查。这是为了防止程序在未设置参数时崩溃。

不幸的是我收到了这个错误:

Notice: Undefined variable: _POST

运行的代码如下:

# check for the right arguments
function checkArguments($type, $arg_array) {
    # $type is either 'GET' or 'POST'
    # $arg_array is an array with all the arguments names

    foreach($arg_array as $arg) {
        print(${"_" . $type}["name"]);
        $type = "_" . $type;
        if(!isset($$type[$arg])) {
            $missing[] = $arg;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将它们分配给临时变量并使用它。

$arr = $_GET;
if ($type == "POST") $arr = $_POST;

答案 1 :(得分:1)

我会假设你想要一个变量变量,我试着避免它们,因为它们很难在代码中读取。它还会破坏(或不起作用)大多数IDE编辑器。

我刚看到的一件事情是相关的。

http://php.net/manual/en/language.variables.variable.php

  

请注意,变量变量不能与PHP的函数或类方法中的超全局数组一起使用。变量$ this也是一个无法动态引用的特殊变量。

$_POST将被计入"超全球" {是$_GET

相关问题