note 42415 added to function.var-export
| From: | php at rn2 dot php dot net | Date: | Sat, 15 May 2004 19:40:08 +0000 |
| Subject: | note 42415 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69652@lists.php.net to get a copy of this message | ||
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 ---
----
Manual Page -- http://www.php.net/manual/en/function.var-export.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42415
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42415&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42415&report=yes
Search -- http://master.php.net/manage/user-notes.php