Home » How to Use PHP Serialize() and Unserialize() Function

How to Use PHP Serialize() and Unserialize() Function

by Online Tutorials Library

How to Use PHP Serialize() and Unserialize() Function

We cannot move, transport, or store complex data in PHP. In cases when we need to execute a set of complex data, we tend to use serialize( ) and unserialize( ) functions.

The serialize function modifies the complex data structures to streamline compatible shapes, which PHP can easily transmit. These reconstructed structures can again be deconstructed using the unserialize( ) function.

Serialize() Function

This PHP function converts a complex data set to a byte stream representation that can be easily stored in PHP. Serialize( ) to save the elements as objects will convert all the available variables into objects.

But the method used inside the objects won’t be saved in the object. Instead, only the name of the class will be present. Once the object is declared to the structure, we must unserialize( ) the created object.

Example

If we create a class employee and then serialize it, PHP will convert the serialized class to a string that will initially point towards the class employees. It will hold all the variables contained inside it.

But to unserialize the created employee class in some other file, it is mandatory to have the definition of employees class present in the first file. This can be accomplished using the function spl_ autoload _ register ( ) function available in PHP.

Syntax

Below is the syntax of serialize() function,

Program

Lets write a program using the serialize() function,

Output

The above code gives the following output,

Arraya:4:{ I :0;s:11:"hello world"; I  :1; I :99; I :2;a:2:{ I :0; I :2; I :1;s:4:"four";} I :3;s:4:"pink";}  A:4:{ I :0;s:26:"this is an array employees"; I :1; I :24500000; I  :2;a:3:{ I :0;s:3:"bmw"; I :1;s:5:" Volvo "; I :2;s:4:"audi";} I :3;s:18:"software developer";}  

In this program, we have created two objects, $myv and $myv2, with different elements and used serialize function to convert the object to a string.

Unserialize() Function

The main objective of this function is to unserialize the pre-sterilized array back to its previous complex structure.

Syntax

Below is the syntax of unserialize() function,

Program

Let’s write a code using the unserialize() function,

Output

The above code gives the following output,

Array a : 4:{ I :0;s:11:"hello world"; I  :1; I :99; I :2;a:2:{ I :0; I :2; I :1;s:4:"four";} I :3;s:4:"pink";}  Array  (      [0] => hello world      [1] => 99      [2] => Array          (              [0] => 2              [1] => four          )        [3] => pink  )  A:4:{ I :0;s:26:"this is an array employees"; I :1; I :24500000; I  :2;a:3:{ I :0;s:3:"bmw"; I :1;s:5:" Volvo "; I :2;s:4:"audi";} I :3;s:18:"software developer";}  Array  (      [0] => this is an array employees      [1] => 24500000      [2] => Array          (              [0] =>bmw              [1] => Volvo  [2] =>audi          )      [3] => software developer  )  

You may also like