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

Source: Internet
Author: User
When you log in PHP, or if the Ajax request is wrong, you want to debug. We usually write the information to a specified file
Of Then, according to the corresponding information to deal with the problem.
For example, the author likes to use Ajax to get no data, in the PHP script, add the following section of code
Copy Code code 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 how to do?
You might say, I'm looping output. What if it's a multidimensional array?
I'm just so tired of the need for Debug.
Here you can use Var_export ().
This function returns the structure information about the variable passed to the function, which is similar to the Var_dump (), unlike the
The representation that it returns is a valid PHP code.
You can return the representation of a variable by setting the second argument of the function to TRUE.
Copy Code code as follows:

$fp = fopen ('./a.txt ', ' a+b ');
Fwrite ($fp, Var_export ($content, true));
Fclose ($FP);

AttentionThe second parameter of Var_export () needs to be set to true to indicate that the return value was obtained. Otherwise, direct output.
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 () is also set to True
Copy Code code 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.