[英]in PHP, is it possible to put function into a variable and let the string into the the variable print first?
[英]Is PHP possible to print out variable source code as string?
我有一个像这样的简单变量
$arr = [
'aa' => 'aa',
'bb' => 'bb'
];
现在,PHP 带有几个打印输出 function
print_r($arr);
结果:
Array
(
[aa] => aa
[bb] => bb
)
var_dump($arr);
结果:
array(2) {
["aa"]=>
string(2) "aa"
["bb"]=>
string(2) "bb"
}
我想要一个可以像这样打印出原始源代码的 function:
print_out_source_code($arr);
结果:
$arr = [
'aa' => 'aa',
'bb' => 'bb'
];
有没有 function 可以做到这一点?
这应该适用于您要求的确切“恢复数组”。
试试“var_export”和“eval”:
$arr = [
'aa' => 'aa',
'bb' => 'bb'
];
$filename = 'arr_test.txt';
// save the export, evaluable code of the variable:
$bytes = file_put_contents($filename, var_export($arr, true));
print_r($arr);
// restoring the arr from saved "source code"
$arr = null;
eval('$arr = '.file_get_contents($filename).';');
print_r($arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.