note 21801 deleted from function.print-r by betz
| From: | betz@php.net | Date: | Sun, 18 Jan 2004 15:39:48 +0000 |
| Subject: | note 21801 deleted from function.print-r by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63787@lists.php.net to get a copy of this message | ||
Note Submitter: bcash@alpineinternet.com
----
Fixed incorrect handling of true, false, and null values. Also,
this showvar will now return a string rather than print for something of
a human-readable serialization if var_export() does not work for you.
function showvar($var, $string = '$var')
{
$out = "";
// TRUE, FALSE, NULL and empty arrays: return this value
if($var === true) {
$out .= htmlentities($string . ' = TRUE;') . "\n";
} else if($var === false) {
$out .= htmlentities($string . ' = FALSE;') . "\n";
} else if($var === null) {
$out .= htmlentities($string . ' = NULL;') . "\n";
} else if($var === array()) {
$out .= htmlentities($string . ' = array();') . "\n";
// array or object - foreach element of $var
} else if ( is_array( $var ) || is_object($var) ) {
foreach($var as $k=>$v) {
// Format the string which stands next 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;
}
// dive
$out .= showvar($v, $new_string);
}
// not object, not array
} else {
// Format as a string if it is one
if(is_string($var)) {
$var = "'" . str_replace('\"', '"',
addslashes($var)) . "'";
}
$out .= htmlentities($string . ' = ' . $var) . ";\n";
}
return $out;
}