PDA

View Full Version : Scope of arrays


bassleader
01-11-2003, 09:44 PM
Hi,

I am trying to use a function to help me debug my scripts, but it seems to have bugs of its own. At the top of my script, after calling session_start() I call the function dump_vars:

function dump_vars()
{
echo "<!-- BEGIN VARIABLE DUMP -->\n\n";

echo "<!-- BEGIN GET VARS -->\n";
echo "<!-- " .dump_array($HTTP_GET_VARS). " -->\n\n";

echo "<!-- BEGIN POST VARS -->\n";
echo "<!-- " .dump_array($HTTP_POST_VARS). " -->\n\n";

echo "<!-- BEGIN SESSION VARS -->\n";
echo "<!-- " .dump_array($HTTP_SESSION_VARS). " -->\n\n";

echo "<!-- BEGIN COOKIE VARS -->\n";
echo "<!-- " .dump_array($HTTP_COOKIE_VARS). " -->\n\n";

echo "<!-- END VARIABLE DUMP -->\n\n\n";
}

The function dump_array($array) looks like this:

function dump_array($array)
{
if (is_array($array))
{
$size = count($array);
$string = "";
if ($size)
{
$count = 0;
//write each element's key and value to to the string
foreach($array as $var => $value)
{
$string .= "$var = $value";
if($count++ < ($size -1))
{
$string .="\n ";
}
}
}

return $string;
}
else
{
return "this is not an array";
}
}

The problem I have is that everything I pass to dump_array returns false for is_array(), therefore all I get is four lines of "this is not an array". Yet I can put a line like:

echo $HTTP_POST_VARS["email"];

into the dump_vars function and get the result I would expect. What am I doing wrong?

BL

Phip
01-11-2003, 10:21 PM
are you calling the function with:

dump_array($HTTP_POST_VARS);

firepages
01-12-2003, 02:23 AM
$HTTP_GET_VARS etc are not global , either use $_GET , $_POST etc if you have php4.1.0+ or globalise them..


<?
function dump_vars(){
global $HTTP_REQUEST_VARS;
echo '<pre>';
var_dump($HTTP_REQUEST_VARS);
var_dump($_SERVER);
echo '</pre>';
}

dump_vars();
?>

bassleader
01-12-2003, 01:10 PM
Thanks guys, I went with using $_GET etc. Is one way better than the other?

BL

Phip
01-12-2003, 08:37 PM
you could have also called the function with an argument.


function dump_array($theArray)
{
echo $theArray[0];
}


dump_array($arrayVar)