Parsing how to write an array variable to a file in PHP

Source: Internet
Author: User
Tags php script

When you want to debug with PHP logging, or an AJAX request error. We generally write information to a specified file
Of The problem is then handled according to the appropriate information.
For example, I like to use Ajax do not get data, in the PHP script and add the following code

Copy CodeThe code is as follows:
$fp = fopen ('./a.txt ', ' a+b ');
Fwrite ($fp, $content);
Fclose ($FP);


However, there is a problem here. That is $content is an array to do?
You might say, I loop the output. What about multidimensional arrays?
I just need to be so tired to debug.
Here you can use Var_export ().
This function returns the structure information about the variable passed to the function, which is similar to Var_dump (), unlike
The representation of its return is a valid PHP code.
You can return the representation of a variable by setting the second argument of the function to TRUE.

Copy CodeThe 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 indicate that the return value is obtained. or the direct output.
Also, if your $content is just an array, and it doesn't contain anything else.
You can also use Print_r ()
Similarly, the second parameter of Print_r () is also set to True

Copy CodeThe code is as follows:
$fp = fopen ('./a.txt ', ' a+b ');
Fwrite ($fp, Print_r ($content, true));
Fclose ($FP);

Parsing how to write an array variable to a file in PHP

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.