Define the use of unserialize in php

A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.


Take for example this common problem:

How do I pass a PHP array to Javascript?

PHP and Javascript can only communicate via strings. You can pass the string "foo" very easily to Javascript. You can pass the number 1 very easily to Javascript. You can pass the boolean values true and false easily to Javascript. But how do you pass this array to Javascript?

Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 ) 

The answer is serialization. In case of PHP/Javascript, JSON is actually the better serialization format:

{ 1 : 'elem 1', 2 : 'elem 2', 3 : 'elem 3' }

Javascript can easily reverse this into an actual Javascript array.

This is just as valid a representation of the same data structure though:

a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}

But pretty much only PHP uses it, there's little support for this format anywhere else.
This is very common and well supported as well though:

<array>
    <element key='1'>elem 1</element>
    <element key='2'>elem 2</element>
    <element key='3'>elem 3</element>
</array>

There are many situations where you need to pass complex data structures around as strings. Serialization, representing arbitrary data structures as strings, solves how to do this.



Definition and Usage

The function unserialize() converts the serialized data back to normal PHP value.

Syntax

mixed unserialize ( string $data , array $options = [] )

Parameters

Sr.NoParameterDescription
1

data

Mandatory. This specifies the serialized string.

2

options

Optional. Any options to be provided to unserialize(), as an associative array. Can be either an array of class names which can be accepted, false to accept no classes, or true to accept all classes. true is default.

Return Values

This function returns converted value which can be a bool, int, float, string, array or object. Suppose passed string is not unserializeable, false is returned and E_NOTICE is issued.

Dependencies

PHP 4 and above.

Example

Following example demonstrates first serializing and the unserializing the data:

<?php
  class test1{
     private $name;
     function __construct($arg){
        $this->name=$arg;
     }
     function getname(){
        return $this->name;
     }
  }
  $obj1=new test1("tutorialspoint");
  $str=serialize($obj1); //first serialize the object and save to a file test,txt
  $fd=fopen("test.txt","w");
  fwrite($fd, $str);
  fclose($fd);

  $filename="test.txt";
  $fd=fopen("test.txt","r");
  $str=fread($fd, filesize($filename));
  $obj=unserialize($str);
  echo "name: ". $obj->getname();
?>

Output

This will produce following result −

name: tutorialspoint

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
)


What is use of unserialize function in PHP?

The unserialize() function converts serialized data back into actual data.

What is unserialize in laravel?

unserialize() takes a single serialized variable and converts it back into a PHP value.

What is serialize array in PHP?

The serialize array function is a built-in function in PHP. The serialization of data means converts a value into a sequence of bits to be stored in a memory buffer, in a file, or transfer across a network. The array is complex data types; we can not see its content directly.

What is meant by object serialization in PHP?

Serializing an object means converting it to a bytestream representation that can be stored in a file. This is useful for persistent data; for example, PHP sessions automatically save and restore objects.