Dokuro
Dokuro

Reputation: 51

Undefined variable: _GET

I am having a problem with PHP that's confusing to me

Namely: Notice: Undefined variable: _GET in /var/www/dd.lo/app/libraries/system/input.php on line 86

pops up when you call:

$this->input->get('test');

The function calls another function (If my approach is bad please do not be mad. I will be happy if you tell me how to do it correctly):

public function get ($index)
{
    return $this->_getArray('_GET', $index);
}

here is the code of the private function:

private function _getArray ($array, $index)
{
    if (isset(${$array}[$index]))
    {
        return ${$array}[$index];
    }
    else
    {
        return NULL;
    }
}

The Input class provides convenient access to _POST, _GET, _COOKIE and _SERVER data and allows you to avoid type checking:

if (isset($_POST['name']))
{
    $name = $_POST['name'];
}
else
{
    $name = NULL;
}

Incidentally, it requests a page at http://dd.lo/?test=dgdsgsdgsdgsd (i.e. $_GET, I asked)

If you write var_dump($_GET); then there is the index 'test'.

Upvotes: 2

Views: 1940

Answers (1)

Alana Storm
Alana Storm

Reputation: 166046

Apologies for the English, but I don't speak or read your language (Russian?). This answer is based on Google translate's version of what you asked.

PHP's super globals ($_GET, $_POST, etc.) are special variables, and it looks like you can't use these variables with PHP's variable variable feature. For example, this works

$foo = ['Hello'];    
$var_name = 'foo';
var_dump($$var_name);

The "variable variable" $$var_name expands as $'foo'/$foo, and the variable dumps correctly.

However, the following does not work

$var_name = '_GET';
var_dump($$var_name);

It appears that whatever magic scope variable variables live in, that scope doesn't include the super globals. You'll need to rethink your approach. One way you might do this is by accepting the actual array instead of a string that's it's name, and specifying a "by reference" parameter in your function to avoid any performance issues

function _getArray(&$array, $key)
{
    if(!is_array($array)) { throw new Exception("Invalid argumnet!");}
    if(array_key_exists($key, $array))
    {
        return $array[$key];
    }

    return NULL;
}

Upvotes: 4

Related Questions