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);
http://www.bkjia.com/PHPjc/327446.htmlwww.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...