将 print_r 的输出写入 txt 文件。PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13361376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write output of print_r in a txt file. PHP
提问by itaka
Possible Duplicate:
Print array to a file
可能的重复:将
数组打印到文件
How can I write the ouput (for example when I use an array), in a file?, I was trying with this:
如何在文件中写入输出(例如,当我使用数组时)?,我正在尝试这样做:
...
print_r($this->show_status_redis_server());
$status_redis = ob_get_contents();
fwrite($file, $status_redis);
...
回答by Mārti?? Briedis
print_r()has a second parameters that if passed as TRUEreturns the output as a string.
print_r()有第二个参数,如果作为传递,TRUE则将输出作为字符串返回。
$output = print_r($data, true);
file_put_contents('file.txt', $output);
You could even cosinder using var_exportfunction, as it provides better information about data types. From print_ryou can't tell if the variable is NULL of FALSE, but var_exportlets use see exactly the data type of a variable.
您甚至可以使用var_export函数cosinder ,因为它提供了有关数据类型的更好信息。从print_r您无法判断变量是否为 FALSE 的 NULL,但var_export让我们准确地查看变量的数据类型。
回答by Leri
print_r($expression [, bool $return = false ])has optional parameter that identifies you want to return string or echo one.
print_r($expression [, bool $return = false ])具有可选参数,用于标识您要返回字符串或回显一个。
$str = print_r($desiredVariable, true);
fwrite($handle, $str);
Also I'd use file_put_contents:
我也会使用file_put_contents:
$content = print_r($yourVar, true);
file_put_contents('file.log', $content);

