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
- Directly with
var_export
return array(...);
the written PHP code, and then read the time with the require
parse.
- Use
serialize
a function to serialize an array, and then use deserialization when reading unserialize
- Used
json_encode
to serialize an array and then read it with json_decode
deserialization
- 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
- Directly with
var_export
return array(...);
the written PHP code, and then read the time with the require
parse.
- Use
serialize
a function to serialize an array, and then use deserialization when reading unserialize
- Used
json_encode
to serialize an array and then read it with json_decode
deserialization
- 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.