note 21147 deleted from function.print-r by alindeman
| From: | alindeman@php.net | Date: | Sat, 19 Apr 2003 16:29:58 +0000 |
| Subject: | note 21147 deleted from function.print-r by alindeman | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-46814@lists.php.net to get a copy of this message | ||
This little function shows a var dump in the php syntax
function showvar($var, $string = '$var')
{
// For: TRUE, FALSE, NULL and empty arrays: Print the value and quit the function
if($var === true)
{
print htmlentities($string . ' = TRUE;') . "\n";
return;
}
if($var === false)
{
print htmlentities($string . ' = FALSE;') . "\n";
return;
}
if($var === null)
{
print htmlentities($string . ' = NULL;') . "\n";
return;
}
if($var === array())
{
print htmlentities($string . ' = Array();') . "\n";
return;
}
// Variable is no Object and no array? -> Print the value and quit the funciton.
if(!is_object($var) && !is_array($var))
{
// Format as a string if it is one
if(is_string($var))
$var = "'" . str_replace('\"', '"',
addslashes($var)) . "'";
print htmlentities($string . ' = ' . $var) . ";\n";
return;
}
// Loop for every element of $var
foreach($var as $k=>$v)
{
// Format the string which stands nex to the ' = '.
// [] for arrays and -> for objects
if(is_array($var))
{
if(is_string($k))
$k = "'" . str_replace('\"', '"',
addslashes($k)) . "'";
$new_string = $string . '[' . $k . ']';
}
else if(is_object($var))
{
$new_string = $string . '->' . $k;
}
// show the value
showvar($v, $new_string);
}
}