Search code examples
phpforeachwarningssuperglobals

Using PHP Superglobals correctly in a loop


I'm trying to clean up some code and am getting a warning

"Do not Access Superglobal $_GET Array Directly"

on a loop that is used to collect what was returned.

foreach ($_GET as $name => $value) {
    $allinfo.= "_GET = $name : $value<br>";
}

Now is is nice and easy to do individual records so

$token = $_REQUEST['token'];

becomes

$token = filter_input(INPUT_REQUEST, 'token');

but I'm slightly stuck on how to fix this for loops.


Solution

  • foreach ($_GET as $name => $value) {
        $allinfo.= "_GET = $name : " . filter_input(INPUT_GET, $name) . "<br>";
    }
    

    OR

    foreach (filter_input_array(INPUT_GET) as $name => $value) {
        $allinfo.= "_GET = $name : $value <br>";
    }