PHP: how to write array variables to files ?. When logging with PHP, or Ajax request error, you want to debug. We usually write information to a specified file. Then, based on the corresponding information, the problem is handled when logs are recorded in PHP, or Ajax request errors require debugging. 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.
$ 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.
$ Fp = fopen ('./a.txt', 'A + B ');
Fwrite ($ fp, var_export ($ content, true ));
Fclose ($ fp );
Note that 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.
$ Fp = fopen ('./a.txt', 'A + B ');
Fwrite ($ fp, print_r ($ content, true ));
Fclose ($ fp );
When a debugging error occurs in an Ajax pull request. We usually write information to a specified file. Then handle the problem according to the corresponding information...