PHP Serialization and deserialization

Source: Internet
Author: User
PHP Serialization and deserialization are the same as java serialization. Java only needs to implement the empty Serializable interface. Serialize () encodes variables and their & #20540; into text form unserialize () restore original variables when serialization is required when you want to serialize PHP) and deserialization

This is the same as the java sequence. Java only needs to implement the empty Serializable interface.


Serialize () encodes variables and their values into text form

Unserialize () restore original variable

Under what conditions should we serialize the data when you want to write the objects in the memory to the hard disk database; when you want to transfer objects on the network;

When the serialized data is placed in a URL and transmitted between pages, you need to call urlencode () to ensure that the URL metacharacters in the URL are processed.

The settings of the margic_quotes_gpc and magic_quotes_runtime parameters affect the data transmitted to unserialize. If magic_quotes_gpc is enabled, the data transmitted in URL, POST variables, and cookies must be processed using stripslashes () before deserialization. if magic_quotes_runtime is enabled, before writing serialized data to a file, you must use addslashes () for processing. before reading these data, you must use stripslashes () for processing:


Array can also be used to serialize an array object.

 Index = $ index; $ this-> name = $ name ;}$ data1 = new Data (1, "hello"); $ data2 = new Data (2, "world"); $ arr = array (); // you can use ArrayObject. // $ Arr = new ArrayObject (); $ arr [0] = $ data1; $ arr [1] = $ data2; $ str = serialize ($ arr ); $ file = fopen ("tmp.txt", "w"); fwrite ($ file, $ str); fclose ($ file); // $ file = fopen ("tmp.txt ", "r"); $ data = file_get_contents ("tmp.txt"); // deserialize the original array object. $ Obj = unserialize ($ data); print_r ($ obj [0]); echo $ obj [0]-> name;?>

The content of tmp.txt is:

a:2:{i:0;O:4:"Data":2:{s:5:"index";i:1;s:4:"name";s:5:"hello";}i:1;O:4:"Data":2:{s:5:"index";i:2;s:4:"name";s:5:"world";}}


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.