PHP serialize


Function

serialize transforms a PHP value into a string:

serialize($value)

unserialize transforms a string back to a PHP value:

unserialize($string)

PHP values

null

serialize(null); // N;

string

serialize('');      // s:0:"";
serialize('hello'); // s:5:"hello";

bool

serialize(true);  // b:1;
serialize(false); // b:0;

int

serialize(42); // i:42;

float

serialize(3.14); // d:3.14;

array

serialize([]);                         // a:0:{}
serialize(['key' => 'value']);         // a:1:{s:3:"key";s:5:"value";}
serialize(['key' => 'value', 1 => 1]); // a:2:{s:3:"key";s:5:"value";i:1;i:1;}

object

serialize((object) ['key' => 'value']);         // O:8:"stdClass":1:{s:3:"key";s:5:"value";}
serialize((object) ['key' => 'value', 1 => 1]); // O:8:"stdClass":2:{s:3:"key";s:5:"value";s:1:"1";i:2;}

Demo

Repl.it:



If you enjoyed this post, please consider supporting this site!