serializing
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
This entry was posted on Tuesday, March 29th, 2005 and is filed under PHP certification. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

November 30th, 2007 at 3:46 am
In the last example, if there is a double quotes or a single quote in the key or the value, those will be included directly and no string escapism is required.
‘He said, “..I wasn\’t thrilled..”‘);
echo serialize($ar);
?>
will output,
a:1:{s:8:”sentence”;s:32:”He said, “..I wasn’t thrilled..”";}
November 30th, 2007 at 3:48 am
The previous code was this.
‘He said, “..I wasn\’t thrilled..”‘);
echo serialize($ar);
? >
May 24th, 2011 at 6:31 pm
I much like it!…
Amazing article. I need you up-to-date your weblog much more often, I just cannot appear to be to own adequate of one’s blog. I saved your weblog in my bookmarks. Would it be feasible to do a guest article sometime?…