Wednesday, August 27, 2014

Serialization / Unserialization in PHP - I

Through Serialization, we can produce a storable representation of a value. PHP offers serialize() function which accepts value of any type except resource type. The serialize() function can accept arrays also. Array/Objects with circular references ( means some values reference to others within the array or objects ) can also be serialized. The serialize() function returns a byte-stream representation of the value and this may include NULL bytes and in case NULL bytes are serialized, it is better to store the serialized representation in BLOB field within Database. If we try to save it in CHAR, VARCHAR or TEXT type fields, those NULL bytes will be removed.

The reverse process i.e converting the serialized format to respective PHP value ( including array/object ), is called unserialization. PHP offers unserialize() function for unserialization.

Now check an example ::

<?php
// Define same Variables
$i = 100;
$j = true;
$k = "sample text";
$l = array("name" => "John", "age" => 23, "salary" => 103.25, "is_adult" => true );

// Serialize and store in other variables
$ii = serialize($i); 
$jj = serialize($j);
$kk = serialize($k);
$ll = serialize($l);

// Print Serialized data
echo "<b>Serialization returns storable strings ::</b><br>";
echo "$ii $jj $kk $ll <br>";

// Unserialize
echo "<b>Unserialization restores :: </b><br>";

// Print what we got after unserialization
echo unserialize($ii) . "<br>";
echo unserialize($jj) . "<br>";
echo unserialize($kk) . "<br>";

// print_r used to print the array
print_r( unserialize($ll) );  
?>

In the above program, we defined some variables of type integer, boolean, string and array and we called the serialize() function on each of them and stored the results in other variables. Next, we called unserialize() function to restore original data from the serialized representation.

Check the output :: 

Serialization returns storable strings ::
i:100; 
b:1; 
s:11:"sample text"; 
a:4:{s:4:"name";s:4:"John";s:3:"age";i:23;s:6:"salary";d:103.25;s:8:"is_adult";b:1;} 

Unserialization restores :: 
100
1
sample text
Array ( [name] => John [age] => 23 [salary] => 103.25 [is_adult] => 1 ) 

See that serialize($i) generates a string like "i:100" which means integer value 100. 
Similarly, "b:1" means Boolean value 1
s:11:"sample text" means String with length: 11 and value: 'sample text'

Let's understand the serialized text a:4:{s:4:"name";s:4:"John";s:3:" age";i:23; s:6:"salary"; d:103.25;s:8:"is_adult";b:1;} created for array $l. Array's key and value - both are sequentially stored.

a:4 means array of 4 values. All the values are wrapped in curly braces.
s:4:"name";       => Key, String of length:4, key value:"name" 
s:4:"John";       => Value, String of length:4, value:"John"
s:3:"age";          => Key, String of length:3, key value:"age"
i:23;                      => Value, Integer 23
s:6:"salary";  => Key, String of length:6, key value:"salary"
d:103.25;            => Value, Double value 103.25
s:8:"is_adult"; => Key, String of length:8, key value:"is_adult"
b:1;                        => Value, Boolean value 1

Let's check some more serialized arrays.

<?php
// Array 1
$arr1 = array("John",20,103.5,true);

// Array 2
// See that the 3rd item would have index '11'
$arr2 = array(10=> "John", 9=> 20, 103.5, 1=>true);

// Array 3
$arr2 = array('name'=> "John", 'age'=> 20, 'is_adult'=>true);

echo unserialize($arr1);
echo unserialize($arr2);
?>

The first output of the above program is ::
a:4:{i:0;s:4:"John";i:1;i:20;i:2;d:103.5;i:3;b:1;}

As said earlier, array's key and value are stored sequentially. 

a:4:                          means array consists of 4 items.
i:0;s:4:"John" means integer key 0 holds string (length:4) value "John".
i:1;i:20                means integer key 1 holds integer value 20.
i:2;d:103.5        means integer key 2 holds double value 103.5.
i:3;b:1                  means integer key 3 holds boolean value true/1.

The 2nd output of the above program is ::
a:4:{i:10;s:4:"John";i:9;i:20;i:11;d:103.5;i:1;b:1;}

i:10;s:4:"John"; means integer Key/index '10' holds a string "John"
i:9;i:20;                  means integer Key/index '9' holds an integer value 20
i:11;d:103.5;        means integer Key/index '11' holds a double value 103.5

The 3rd output of the above program is ::
a:3:{s:4:"name";s:4:"John";s:3:"age";i:20;s:8:"is_adult";b:1;}

s:4:"name";s:4:"John"; means string (length:4) key "name" holds string value "John" (length:4)
s:3:"age";i:20;              means string (length:3) key "age" holds integer value 20
s:8:"is_adult";b:1;    means string (length:8) key "is_adult" holds boolean value true/1

The unserialize() function converts the stored representation to appropriate PHP value. This function returns FALSE if the passed string is not unserializable. 

Check out the next part of this article : Serialization / Unserialzation in PHP - II

No comments: