note 21801 added to function.print-r
| From: | bcash at alpineinternet dot com | Date: | Mon, 27 May 2002 23:04:05 +0000 |
| Subject: | note 21801 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31079@lists.php.net to get a copy of this message | ||
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;
}
--
http://www.php.net/manual/en/function.print-r.php
http://master.php.net/manage/user-notes.php?action=edit+21801
http://master.php.net/manage/user-notes.php?action=delete+21801
http://master.php.net/manage/user-notes.php?action=reject+21801