serializing

March 29th, 2005. Tagged: PHP certification

Serializing is a nice way to get a string out of more complicated data structure. It's probably mostly used to sting-alize array data, although you can serialize scalar data as well.

The result of the serialization is no nature wonder, it's just a string that describes what is serialized and a value of the serialized ... thing.

The cert guide explains it all very well, I think just maybe an example of how something looks when it's serialized would have been nice. Otherwise the serialization may sound scary and mysterious.

Here's an example:
< ?php $s = 'PHP'; echo serialize($s); ?>
This prints:
s:3:"PHP";
Which means:

  • 's' is a string, the type of the serialized var
  • '3' is the length
  • 'PHP' is the value
  • ';' is a delimiter

A slightly more complicated example - serializing an array:

< ?php $ar = array('PHP','key'=>'serialize');
echo serialize($ar);
?>
The result is:
a:2:{i:0;s:3:"PHP";s:3:"key";s:9:"serialize";}

  • 'a' means an array
  • 'i' as in integer
  • 'i:0' is the integer key of the first element of the array, remember when you don't specify a key, PHP assigns one

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov