Php serialize and unserialize syntax

Source: Internet
Author: User
Tags mixed php tutorial

Php Tutorial serialize and unserialize syntax
Serialize --------- format the array into an ordered string

Unserialize

(Php 3> = 3.0.5, php 4)

Unserialize -- create a php value from a stored representation

Description
Mixed unserialize (string str [, string callback])
Unserialize () operates on a single serialized variable and converts it back to the php value. The returned value is the converted value, which can be integer, float, string, array, or object. If the passed string cannot be deserialized, false is returned.

Unserialize_callback_func command: if an undefined class needs to be instantiated during deserialization, you can set a callback function for calling (to avoid the incomplete object "_ php_incomplete_class "). You can use php. ini, ini_set (), or. htaccess to define 'unserialize _ callback_func '. It is called every time an undefined class is instantiated. To disable this feature, you only need to leave this setting empty. Note that the unserialize_callback_func command is provided from php 4.2.0.

Unserialize ----- restore an array to an array

Serialize

(Php 3> = 3.0.5, php 4)

Serialize -- generate a representation of a stored value

Description
String serialize (mixed value)
Serialize () returns a string that contains a byte stream that represents value and can be stored anywhere.

This facilitates storing or passing php values without losing their types and structures.

To change serialized strings back to php values, use unserialize (). Serialize () can process any type other than resource. Even serialize () contains arrays that point to its own reference. References in arrays/objects of serialize () will also be stored.

When an object is serialized, php tries to call the member function _ sleep () of the object before the sequential action (). This allows objects to be cleared before being serialized. Similarly, when unserialize () is used to restore an object, the _ wakeup () member function is called.
Note: in php 3, object attributes will be serialized, but methods will be lost. Php 4 breaks this restriction and can store attributes and methods at the same time. For more information, see the serialization object section of classes and objects.


Test instance:
$ Test = array ("a" => 0, "B" => 0, "c" => 0 );
$ Test2 = '';
$ Test2 = serialize ($ test );
Echo $ test2;
Echo "Print_r (unserialize ($ test2 ));


Purpose:
In my personal experience, I mainly deal with array transfer and array inventory operations.
For example, I have an array that needs to be passed to the next page. If you do not want to use seesion/cookie, you can use this function to pass and restore it.
For example, when I make a website directory, there is a score, which can be divided into high praise, medium rating, and poor rating. In my database tutorial, there is only one field designed for this function, and the type is long standby. Combine the three comments into an array:
Array (
'A' => 0, // 0
'B' => 0, // 0
'C' => 0 // 0 negative ratings
)

After converting it with the serialize function, it is: a: 3: {s: 1: "a"; I: 0; s: 1: "B"; I: 0; s: 1: "c"; I: 0;}, and then the database exists. Do not forget to use
Use the unserialize function to convert to an array

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.