将print_r的输出写入txt文件.的PHP

问题描述:

可能重复:
将数组打印到文件中

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);
    ...

print_r()具有第二个参数,如果作为TRUE传递,则将其作为字符串返回输出.

print_r() has a second parameters that if passed as TRUE returns the output as a string.

$output = print_r($data, true);
file_put_contents('file.txt', $output);

您甚至可以使用var_export函数进行强制转换,因为它提供了有关数据类型的更好信息.在print_r中,您无法确定变量是否为FALSE的NULL,但是var_export允许使用来确切地查看变量的数据类型.

You could even cosinder using var_export function, as it provides better information about data types. From print_r you can't tell if the variable is NULL of FALSE, but var_export lets use see exactly the data type of a variable.