![](/img/trans.png)
[英]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.