Specifications

LISTING 23.1 dump_variables.phpThis Code Can Be Included in Pages to Dump the
Contents of Variables for Debugging
<?
// these lines format the output as HTML comments
// and call dump_array repeatedly
echo “\n<!-- BEGIN VARIABLE DUMP -->\n\n”;
echo “<!-- BEGIN GET VARS -->\n”;
echo “<!-- “.dump_array($HTTP_GET_VARS).” -->\n”;
echo “<!-- BEGIN POST VARS -->\n”;
echo “<!-- “.dump_array($HTTP_POST_VARS).” -->\n”;
echo “<!-- BEGIN SESSION VARS -->\n”;
echo “<!-- “.dump_array($HTTP_SESSION_VARS).” -->\n”;
echo “<!-- BEGIN COOKIE VARS -->\n”;
echo “<!-- “.dump_array($HTTP_COOKIE_VARS).” -->\n”;
echo “\n<!-- END VARIABLE DUMP -->\n”;
// dump_array() takes one array as a parameter
// It iterates through that array, creating a string
// to represent the array as a set
function dump_array($array)
{
if(is_array($array))
{
$size = count($array);
$string = “”;
if($size)
{
$count = 0;
$string .= “{ “;
// add each element’s key and value to the string
foreach($array as $var => $value)
{
$string .= “$var = $value”;
if($count++ < ($size-1))
{
$string .= “, “;
}
}
Debugging
C
HAPTER 23
23
DEBUGGING
487
29 7842 CH23 3/6/01 3:41 PM Page 487