Home >Backend Development >PHP Tutorial >Analyze how to write array variables to files in PHP_PHP Tutorial

Analyze how to write array variables to files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:08:29839browse

When using PHP to record logs, or when an Ajax request error occurs and you want to debug it. We usually write information to a specified file
. 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

Copy the code The code is as follows:

$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 multi-dimensional array?
Am I so tired just for debugging?
Here you can use var_export().
This function returns structural information about the variable passed to the function. It is similar to var_dump(), except that
the representation returned is legal PHP code .
You can return a representation of a variable by setting the second parameter of the function to TRUE.
Copy code The code is as follows:

$fp = fopen('./a.txt', 'a+b' );
fwrite($fp, var_export($content, true));
fclose($fp);

Note The second parameter of var_export() needs to be set to true to obtain the return value. Otherwise, output it 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() Also set to true
Copy code The code is as follows:

$fp = fopen('./a.txt', 'a+b');
fwrite($fp, print_r($content, true));
fclose($fp);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327446.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...
Statement:
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