note 21147 added to function.print-r
| From: | henrik dot gebauer at web dot de | Date: | Tue, 30 Apr 2002 21:44:22 +0000 |
| Subject: | note 21147 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-30029@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);
}
}
--
http://www.php.net/manual/en/function.print-r.php
http://master.php.net/manage/user-notes.php?action=edit+21147
http://master.php.net/manage/user-notes.php?action=delete+21147
http://master.php.net/manage/user-notes.php?action=reject+21147