What's the best way to save an array of PHP in a file?

Source: Internet
Author: User
It is commonplace for PHP to save some array of configuration information to a file, as far as I know, there are several ways to save

    1. Directly with var_export return array(...); the written PHP code, and then read the time with the require parse.
    2. Use serialize a function to serialize an array, and then use deserialization when reading unserialize
    3. Used json_encode to serialize an array and then read it with json_decode deserialization
    4. Use a different file format, for example, yaml orini

Has anyone compared the pros and cons of these kinds of methods? Which methods are better, or are they suitable for different scenarios?

Reply content:

It is commonplace for PHP to save some array of configuration information to a file, as far as I know, there are several ways to save

    1. Directly with var_export return array(...); the written PHP code, and then read the time with the require parse.
    2. Use serialize a function to serialize an array, and then use deserialization when reading unserialize
    3. Used json_encode to serialize an array and then read it with json_decode deserialization
    4. Use a different file format, for example, yaml orini

Has anyone compared the pros and cons of these kinds of methods? Which methods are better, or are they suitable for different scenarios?

If your array is just for PHP projects, using the first method is the best and most convenient and I've been using it

If your data might not just be used by PHP, it's better to use a more versatile JSON format, of course yaml,xml, but it's not as easy as json_encode.

Serialize I really rarely use it when I save an array.

PS: Sorry just didn't see you are talking about the use of the configuration file. If it is a configuration file, it is important to note that your PHP code will probably be encrypted, if it is possible to use JSON or yml or INI and other formats to facilitate the configuration file modification

I think the first way is not very good, this may cause the execution of arbitrary PHP Code vulnerability.

JSON is recommended for readability (compared to serialization), readable and writable (PHP does not have built-in functions to write INI), good versatility, and other languages can be accessed more easily.

In general, ini it is best to use (manual), followed by serialize (Automatic system generation)

I generally use JSON, cross-platform, and easy to read.

If you need a large compression, you can choose Apple binary plist format, the disadvantage is that the naked eye is unreadable, need to introduce cfpropertylist.

General requirements are available in JSON or XML. Cfpropertylist can also be used to write or parse XML

If it is for PHP only, I prefer to use the PHP array directly.

Serialize

It has to be this way, because it's so simple that you just need to serialize it. It is more convenient than josn (if you use it in a PHP project and not for other non-PHP projects) recommend serialize.

Let's use JSON. Readability is strong. Small size. Fast parsing speed. Cross-language.

XML, though, also has great readability. But it's too big for JSON, and parsing speed is a problem.
Yaml is generally readable, and more complex content looks to the naked eye as a bunch of heavenly book. But there is no doubt that the file size will be minimal.

Except Var_export. You need to consider the issue of files being opened directly by the browser.

Eventually recommend Json,json_encode ($content, 15). It is recommended to use a second parameter. Json_decode ($data, True) can easily convert an object to an array type. Easy to operate.

Look at the demand.

  • Related Article

    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.