How to write array variables to file in PHP? _PHP Tutorial

WBOY
Release: 2016-07-14 10:08:52
Original
929 people have browsed it

When using PHP to record logs, or when an Ajax request error occurs and you want to debug it. We generally write information to a specified file

Among them. Then deal with the problem based on the corresponding information.
For example, when I cannot get data using Ajax, I like to add the following code to the PHP script
$fp = fopen('./a.txt', 'a+b');
fwrite($fp, $content);
fclose($fp);
However, there is a problem. That is, what if $content is an array?
You might say, I loop the output. What if it’s a multidimensional array?
Do I need to be so tired just for debugging?
Here you can use var_export() .
This function returns structural information about the variables passed to this function. It is similar to var_dump(), except that
The representation returned is valid PHP code.
You can return a representation of a variable by setting the second parameter of the function to TRUE.
$fp = fopen('./a.txt', 'a+b');
fwrite($fp, var_export($content, true));
fclose($fp);
Note that the second parameter of var_export() needs to be set to true to obtain the return value. Otherwise, output directly
In addition, if your $content is just an array and does not contain other content
You can also use print_r()
Similarly, the second parameter of print_r() must also be set to true
$fp = fopen('./a.txt', 'a+b');
fwrite($fp, print_r($content, true));
fclose($fp);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477743.htmlTechArticleWhen using PHP to record logs, or when an Ajax request error occurs and you want to debug it. We generally write information to a specified file. Then deal with the problem based on the corresponding information...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template