I am trying to code a simple debug function which will list the contents of $GET and $POST arrays.
Below is a simplification of my code (error checking, style support, etc, removed).
function debugArrayItem($value, $key) {
echo("<LI>$key: $value</LI>");
}
function debugArrayGlobal($arrayName) {
echo("<P>$".$arrayName."</P>/n<UL>");
array_walk($$arrayName, 'debugArrayItem');
echo("</UL>");
}
debugArrayGlobal('_GET');
debugArrayGlobal('_POST');
This sticking point is trying to pass the arrays to array_walk using $$arrayName. It doesn't work.
however if I modify the function as follows:
function debugArrayGlobal($arrayName) {
$arrayName = 'array';
$array = array('a1' => 1, 'a2' => 123, 'help' => 2345);
echo("<P>$".$arrayName."</P>/n<UL>");
array_walk($$arrayName, 'debugArrayItem');
echo("</UL>");
}
the function will list the values of $array. Similarly if I use switch ($arrayName) to directly access $GET and $POST the function works fine.
Is there any reason I can't access PHP super globals via the $$ method?
Is there an alternative method to access these whilst still being able to output the superglobal array name - without passing both the array and its name to my debug function?