How to write an array variable to a file in PHP _php tutorial

Source: Internet
Author: User

How to write an array variable to a file in PHP


When you want to debug with PHP logging, or an AJAX request error. We generally write information into a specified file. The problem is then handled according to the appropriate information.

For example, the author likes to use Ajax to get the data, in the PHP script, add the following code:

$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, similar to Var_dump (), unlike the PHP code whose returned representation is normal.

You can return the representation of a variable by setting the second argument of the function to TRUE.

$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. Otherwise the direct output.

Also, if your $content is just an array and does not contain anything else, you can use Print_r (), and the second parameter of Print_r () is also set to true.

$fp =fopen ('./a.txt ', ' a+b '); fwrite ($fp, Print_r ($content, True)); fclose ($FP);

Articles you may be interested in

    • Introduction to the set, intersection, and difference functions of arrays in PHP
    • PHP error_log () writes error information to a file
    • Differences in PHP after adding the static keyword to variables and functions
    • Summary of methods used by PHP to save arrays as text formats
    • PHP generates consecutive numbers (Letters) array function range () analysis, PHP lottery program function
    • Summary of methods for removing duplicate values in PHP two-dimensional arrays
    • How to solve the problem of concurrent read and write file conflicts in PHP
    • How PHP gets the first array cell value of an array

http://www.bkjia.com/PHPjc/991316.html www.bkjia.com true http://www.bkjia.com/PHPjc/991316.html techarticle How to write an array variable in PHP to a file in a log in PHP, or an AJAX request error when you want to debug. We generally write information into a specified file ...

  • 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.