php 将 print_r 结果作为字符串或文本存储到变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9325067/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:34:38  来源:igfitidea点击:

Store print_r result into a variable as a string or text

php

提问by aWebDeveloper

If I use print_r or var_dumpit displays the result on the screen, but I want this data to be stored in a variable so that I can write it to a file.

如果我使用print_r 或var_dump它在屏幕上显示结果,但我希望将此数据存储在一个变量中,以便我可以将其写入文件。

How do I do this?

我该怎么做呢?

回答by giker

   $var = print_r($what, true);

You must add true into print_r.

您必须将 true 添加到print_r 中

回答by Zaffar Saffee

What you do while you print or dump? Basically you send your data (result or anything) to Show it on screen. Keep your mind clear that its not saved, it is just displayed, To save the data , so a simple thing, just declare a variable and assign the data to it..

你在打印或转储时做什么?基本上,您将数据(结果或任何内容)发送到在屏幕上显示。请记住,它没有保存,它只是显示出来,为了保存数据,这么简单的事情,只需声明一个变量并将数据分配给它即可。

for example you are printing some array like this..

例如你正在打印一些这样的数组..

print_r(myArray);

to save this, you just have to add an option , set Return to TRUE and assign it to a variable

要保存它,您只需添加一个选项,将 Return 设置为 TRUE 并将其分配给一个变量

$myVariable=print_r(myArray, TRUE);

if you need some more information, Follow this

如果您需要更多信息,请关注

hoping this will help you understanding the concept

希望这能帮助你理解这个概念

回答by selmonal

ob_start();
var_dump($someVar);
$result = ob_get_clean();

it works.

有用。