This article provides a detailed analysis of how to write array variables to files in PHP. For more information, see 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 the problem is handled according to the corresponding information.
For example, I like to add the following code in the PHP script when Ajax cannot retrieve data.
The code is as follows:
$ Fp = fopen ('./a.txt', 'A + B ');
Fwrite ($ fp, $ content );
Fclose ($ fp );
However, there is a problem here. What if $ content is an array?
You may say that I loop the output. What about multi-dimensional arrays?
Is it so tiring for debugging.
Here you can use var_export ().
This function returns the structure information about the variables passed to this function. it is similar to var_dump (). The difference is that
The returned result is a valid PHP code.
You can set the second parameter of the function to TRUE to return the expression of the variable.
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 () must be set to true to return the value. Otherwise, it is a direct output.
In addition, if your $ content is just an array and does not contain any other content
You can also use print_r ()
Similarly, the second parameter of print_r () must be set to true.
The code is as follows:
$ Fp = fopen ('./a.txt', 'A + B ');
Fwrite ($ fp, print_r ($ content, true ));
Fclose ($ fp );