note 42415 deleted from function.var-export by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 17 May 2004 06:31:54 +0000 |
| Subject: | note 42415 deleted from function.var-export by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69691@lists.php.net to get a copy of this message | ||
Note Submitter: php (at) burtsev .dot. com
----
I wrote a following bit of code that I think many will find useful, especially if outputing to a
file. It does pass the var_export($GLOBALS); or even references to $GLOBALS array.
example:
--- in START ---
$a = array (1, 2, array ("a", "b", "c"), 'bedboy' =>
&$GLOBALS, $GLOBALS);
var_export($a);
--- in END ---
--- out START ---
array(
0 => 1,
1 => 2,
2 => array(
0 => "a",
1 => "b",
2 => "c"
),
"bedboy" => &$GLOBALS
3 => &$GLOBALS
)
--- out END ---
In case of var_export($GLOBALS);, it will just output '"GLOBALS" =>
&$GLOBALS' where the globals array was.
Code:
//--- Start ---
if (!function_exists("var_export")) {
function var_export($array, $return = false, $tabs = "")
{
if ( is_array($array) )
{
$out .= "array(\n";
$keys = array_keys($array);
$num = count($keys);
for ($i = 0; $i < $num; $i++)
{
if (is_string($keys[$i]))
$out .= $tabs . "\t" . '"' . $keys[$i] . '" => ';
else
$out .= $tabs . "\t" . $keys[$i] . ' => ';
if (!isset($array[$keys[$i]]["GLOBALS"]["GLOBALS"]["GLOBALS"]))
{
switch(gettype($array[$keys[$i]]))
{
case "array":
$out .= var_export($array[$keys[$i]], true, $tabs . "\t");
break;
case "string": case "NULL":
$out .= '"' . $array[$keys[$i]] . '"';
break;
default:
$out .= $array[$keys[$i]];
break;
}
$out .= ($i + 1 == $num ? "\n" : ",\n");
} else
$out .= '&$GLOBALS' . "\n";
}
$out .= $tabs . ')';
} else
$out = $array;
if ($return)
return $out;
echo $out;
}
}
//--- END ---