Serialization is the process of converting a variable into a string that can be saved or transmitted, and deserialization is the conversion of the string to the original variable at the appropriate time. Together, these two processes make it easy to store and transfer data, making the program more maintainable. K7 Casino
1. Serialize and Unserialize functions
These two are common functions for serializing and deserializing data in PHP.
View Source print?
03 |
$a = array ( ‘a‘ => ‘Apple‘ , ‘b‘ => ‘banana‘ , ‘c‘ => ‘Coconut‘ ); |
08 |
//输出结果:a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";} |
16 |
//输出结果 Array ( [a] => Apple [b] => banana [c] => Coconut ) |
Problems can occur when an array value contains characters such as double quotes, single quotes, or colons, which are deserialized. To overcome this problem, a clever trick is to use Base64_encode and Base64_decode.
View Source print?
3 |
$s = base64_encode (serialize( $obj )); |
5 |
$original = unserialize( base64_decode ( $s )); |
But base64 encoding will increase the length of the string. To overcome this problem, you can use it with gzcompress.
View Source print?
03 |
function my_serialize( $obj ) |
05 |
return base64_encode (gzcompress(serialize( $obj ))); |
09 |
function my_unserialize( $txt ) |
11 |
return unserialize(gzuncompress( base64_decode ( $txt ))); |
2. Json_encode and Json_decode
Serializing and deserializing using JSON format is a good choice:
- Using Json_encode and Json_decode format output is much faster for serialize and unserialize formats.
- The JSON format is readable.
- The JSON format is smaller than the serialize return data result.
- The JSON format is open and portable. It can also be used in other languages.
View Source print?
01 |
$a = array ( ‘a‘ => ‘Apple‘ , ‘b‘ => ‘banana‘ , ‘c‘ => ‘Coconut‘ ); |
06 |
//输出结果:{"a":"Apple","b":"banana","c":"Coconut"} |
In the above example, the Json_encode output length is obviously shorter than the serialize output length in the previous example.
3. Var_export and Eval
The Var_export function outputs the variable as a string, and Eval executes the string as PHP code, deserializing the contents of the original variable.
View Source print?
01 |
$a = array ( ‘a‘ => ‘Apple‘ , ‘b‘ => ‘banana‘ , ‘c‘ => ‘Coconut‘ ); |
04 |
$s = var_export( $a , true); |
06 |
//输出结果: array ( ‘a‘ => ‘Apple‘, ‘b‘ => ‘banana‘, ‘c‘ => ‘Coconut‘, ) |
11 |
eval ( ‘$my_var=‘ . $s . ‘;‘ ); |
4. Wddx_serialize_value and WDDX Deserialize
The Wddx_serialize_value function can serialize array variables and output them as XML strings.
View Source print?
01 |
$a = array ( ‘a‘ => ‘Apple‘ , ‘b‘ => ‘banana‘ , ‘c‘ => ‘Coconut‘ ); |
04 |
$s = wddx_serialize_value( $a ); |
07 |
//输出结果(查看输出字符串的源码):<wddxPacket version=‘1.0‘> |
12 |
$o = wddx_deserialize( $s ); |
15 |
//输出结果:Array ( [a] => Apple [b] => banana 1 => Coconut ) |
As you can see, there are more XML tag characters, resulting in a lot of space for serialization of this format.
Summary
All of the above functions perform normally when serializing array variables, but they are different when applied to objects. For example, the Json_encode serialization object fails. When deserializing an object, Unserialize and eval will have different effects.
PHP multiple serialization/deserialization methods