将print_r的输出写入txt文件.的PHP [英] write output of print_r in a txt file. PHP

查看:471
本文介绍了将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.

这篇关于将print_r的输出写入txt文件.的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆