Multiple serialization/deserialization methods for PHP

Source: Internet
Author: User
Tags eval json php code serialization

Serialization is the process of converting a variable to a string that can be saved or transmitted, and deserialization is the conversion of the string to the original variable at the appropriate time. These two processes combine to easily store and transfer data, making programs more maintainable.

1. Serialize and Unserialize functions

These two are common functions for serializing and deserializing data in PHP.

<?php

$a = Array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ');

Serializing an array of

$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);

Print_r ($o);

Output result Array ([a] => Apple [b] => Banana => coconut)

?>

Deserialization
$o = Unserialize ($s);
Print_r ($o);
Output result Array ([a] => Apple [b] => Banana => coconut)
?>

Problems may occur when array values contain characters such as double quotes, single quotes, or colons, which are deserialized. An ingenious technique to overcome this problem is to use Base64_encode and Base64_decode.

$obj = Array ();
Serialization of
$s = Base64_encode (serialize ($obj));
Deserialization
$original = Unserialize (Base64_decode ($s));
However, the Base64 encoding will increase the length of the string. To overcome this problem, you can use it with gzcompress.

Defines a function to serialize an object

function My_serialize ($obj)
{
Return Base64_encode (Gzcompress (Serialize ($obj)));
}
Deserialization
function My_unserialize ($txt)
{
Return Unserialize (Gzuncompress (Base64_decode ($txt)));
}
2. Json_encode and Json_decode

Using the JSON format for serialization and deserialization is a good choice:

Using Json_encode and Json_decode format output is much faster than the serialize and unserialize formats.

The JSON format is readable.

The JSON format is smaller than the result of the serialize return data.

The JSON format is open and portable. It can also be used in other languages.


$a = Array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ');
Serializing an array of
$s = json_encode ($a);
Echo $s;
Output result: {"a": "Apple", "B": "Banana", "C": "Coconut"}
Echo ' <br/><br/> ';
Deserialization
$o = Json_decode ($s);
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; eval executes the string as a PHP code, deserializing the contents of the original variable.


$a = Array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ');

Serializing an array of

$s = Var_export ($a, true);

Echo $s;

Output results: Array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ',)

Echo ' <br/><br/> ';

Deserialization

Eval (' $my _var= '. $s. ';');

Print_r ($my _var);

4. Wddx_serialize_value and WDDX Deserialize

The Wddx_serialize_value function can serialize an array variable and output it as an XML string.


$a = Array (' A ' => ' Apple ', ' B ' => ' banana ', ' C ' => ' coconut ');

Serializing an array of
$s = wddx_serialize_value ($a);
Echo $s;

Output (view source of output String): <wddxpacket version= ' 1.0 ' >
Echo ' <br/><br/> ';

Deserialization
$o = Wddx_deserialize ($s);
Print_r ($o);
Output result: 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 correctly when serializing an array variable, but they are different when applied to an object. For example, Json_encode serialization of an object fails. Unserialize and Eval will have different effects when deserializing an object.

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.