3 ways to serialize variables in php

By | July 14, 2012

The idea behind serializing a variable is to create a storable string format of it, such that the same variable can be recreated later when required using the string.

1. serialize and unserialize

These are the traditional functions to serialize and unserialize data in PHP.

$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');

//serialize the array
$s = serialize($a);
echo $s;
//a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";}

echo '<br /><br />';

//unserialize
$o = unserialize($s);

print_r($o);

When the array values contain characters like the double quote, single quote, or colon, there might be problems when they are unserialized after being fetched from a database. In order to overcome this problem a quick trick is to use base64_encode and base64_decode.

<?php 
$obj = array();
//serialize
$s = base64_encode(serialize($obj)); 
//unserialize
$original = unserialize(base64_decode($txt));

But base64 encoding will increase the size of the string. To overcome this, gzcompress can be used together.

//function to serialize an object
function my_serialize( $obj ) 
{ 
   return base64_encode(gzcompress(serialize($obj))); 
} 

//function to unserialize the serialized text
function my_unserialize($txt) 
{ 
   return unserialize(gzuncompress(base64_decode($txt))); 
}

2. json_encode and json_decode

JSON format is a good alternative to serialize/unserialize because :

1. json_encode + json_decode is much faster than serialize + unserialize
2. json format is readable.
3. json format is smaller than serialized result.
4. json format is open and portable. Other languages can use it as well.

$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');

//serialize the array
$s = json_encode($a);
echo $s;
//{"a":"Apple","b":"banana","c":"Coconut"}

echo '<br /><br />';

//unserialize
$o = json_decode($s);

In the above example the output of json_encode is clearly shorter in length compared to output of serialize in the earlier example.

3. var_export and eval

Php has a function called var_export, that will export a variable in php syntax that can be eval'd to produce the same variable again.

Quick Example

$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');

//serialize the array
$s = var_export($a , true);
echo $s;
//strin is >> array ( 'a' => 'Apple', 'b' => 'banana', 'c' => 'Coconut', )

echo '<br /><br />';

//unserialize
eval('$my_var=' . $s . ';');

print_r($my_var);

4. wddx_serialize_value and wddx_deserialize

These two functions will serialize an array into an xml string. Quick example

$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');

//serialize the array
$s = wddx_serialize_value($a);
echo $s;

//<wddxPacket version='1.0'><header/><data><struct><var name='a'><string>Apple</string></var><var name='b'><string>banana</string></var><var name='c'><string>Coconut</string></var></struct></data></wddxPacket>

echo '<br /><br />';

//unserialize
$o = wddx_deserialize($s);

print_r($o);
//Array ( [a] => Apple [b] => banana [c] => Coconut )

As can be seen, this xml format of serialization takes quite a lot of space, since the the tags make up a lot of characters.

Conclusion

All the above techniques work equally well only in case of arrays. When working with objects the results are quite different. For example json_encode fails with objects. unserialize and eval will have different effects when unserializing objects.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

6 Comments

3 ways to serialize variables in php
  1. Shane

    Great article – serialization is restricted by my host, and all of these alternatives should work. Thank you!

  2. Fadi (itoctopus)

    I wonder whether point #2 and point #3 are truly serializing the variables – I’ve only used serialize($str) and unserialize($str) to serialize/unserialize variables – I suspect most (if not all) programmers do.

    I think the Eval serialize/unserialize is completely inefficient.

Leave a Reply

Your email address will not be published. Required fields are marked *