Parse how PHP writes array variables to files

Source: Internet
Author: User
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.