Serialization is the process of converting a variable to a string that can be saved or transmitted. Deserialization is the conversion of this string into the original variable at the appropriate time. These two processes are combined together. The ability to store and transfer data easily to make the program more maintainable.
1. Serialize and Unserialize functions
These two are frequently used functions for serializing and deserializing data in PHP.
<?php$a = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' Coconut '); Serialized array $s = serialize ($a); echo $s;//output result: a:3:{s:1: "a"; S:5: "Apple"; s:1: "B"; S:6: "Banana"; s:1: "C"; s:7: "Coconut";} Echo ' <br/><br/> ';//deserialization $o = unserialize ($s);p rint_r ($o);//output result Array ([a] + Apple [b] = banana [C] = Coconut)?>
When an array value includes characters such as a double-argument, a single-argument, or a colon. After they are deserialized. Failure may occur. In order to overcome this problem. A clever trick is to use Base64_encode and Base64_decode.
However, the Base64 encoding will add the length of the string.
In order to overcome this problem. Can be used in conjunction with the gzcompress.
Defines a function my_serialize ($obj) { return Base64_encode (gzcompress (Serialize ($obj))) that is used to serialize an object;}// Deserialization function My_unserialize ($txt) { return unserialize (gzuncompress (Base64_decode ($txt)));}
2. Json_encode and Json_decode
Serializing and deserializing using JSON format is a good choice:
$a = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' Coconut '); Serialized array $s = json_encode ($a); echo $s;//output result: {"a": "Apple", "B": "Banana", "C": "Coconut"}echo ' <br/><br/> '; /Deserialization $o = Json_decode ($s);
In the example above. The Json_encode output length is obviously shorter than the serialize output length in the last example.
3. Var_export and Eval
The Var_export function outputs a variable as a string. Eval runs the string as PHP code. Deserialization gets the contents of the original variable.
$a = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' Coconut '); Serialized array $s = Var_export ($a, true); echo $s;//output: Array (' a ' = ' Apple ', ' b ' = ' banana ', ' c ' = ' Coconut ',) ech O ' <br/><br/> ';//Deserialize eval (' $my _var= '. $s. ‘;‘); Print_r ($my _var);
4. Wddx_serialize_value and WDDX Deserialize
The Wddx_serialize_value function can serialize array variables and output them as XML strings.
View Sourceprint?
$a = Array (' a ' + = ' Apple ', ' b ' = ' banana ', ' c ' = ' Coconut ');//serialized Array $s = wddx_serialize_value ($a); echo $s;//Output junction (View source of output String): <wddxpacket version= ' 1.0 ' >
It can be seen that there are many XML tag characters, resulting in the serialization of such a format or occupy a lot of space.
SummaryAll of the above functions work correctly when serializing array variables. But the application to the object is different. For example, the Json_encode serialization object will fail. When deserializing an object. Unserialize and Eval will have different effects.
PHP multiple serialization/deserialization methods