PHP Reference ยท Functions and Classes
PHP serialize() and unserialize()
Reference for PHP serialize() and unserialize(): the text format, objects, __serialize and __unserialize, allowed_classes, failures and security risks.
serialize() turns almost any PHP value into a string that unserialize() can turn back into an equal value. It handles nested arrays and objects, including references between them, which json_encode() cannot do. The format is PHP-specific.
Use it for caches and sessions on a trusted server. Never call unserialize() on data that a user can modify: it can create objects of any class you have loaded, and their magic methods run.
serialize(mixed $value): string unserialize(string $data, array $options = []): mixed
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
serialize(mixed $value): string unserialize(string $data, array $options = []): mixed
| Name | Description |
|---|---|
$value |
Any value except resources and closures (and some built-in objects). |
$data |
A string produced by serialize(). |
$options['allowed_classes'] |
false, true (default) or an array of class names that may be created. Others become __PHP_Incomplete_Class. |
$options['max_depth'] |
Maximum nesting depth (default 4096). |
Return value: serialize() returns a string. unserialize() returns the value, or false on failure (and emits a warning in PHP 8.3 and later, a notice before). Because false is also a valid result, compare the input with serialize(false) when you must tell the cases apart.
Exceptions: serializing a Closure, generator or other unserializable object throws an Exception. A throwing __unserialize() propagates.
Usage notes
- Never unserialize untrusted input. Even with
allowed_classes, prefer JSON (json_decode()) for data crossing a trust boundary. - Prefer
__serialize()and__unserialize()(PHP 7.4) over the older__sleep()and__wakeup(). They return and receive an array and give full control. If both pairs exist, the new ones win. - Private and protected properties are stored with their visibility encoded (special bytes in the name), so the string contains characters you cannot see.
- Strings store byte counts, not characters (
s:6:"hรฉllo"), so editing a serialized string by hand breaks it. - References and shared objects are preserved: two variables that pointed at one object still do after the round trip.
- Enums serialize as
E:entries and come back as the same case. - Class changes break old data. Adding or removing properties can make restored objects incomplete; version your data or use
__unserialize(). - Size and speed. Compared with JSON it is faster for large PHP structures but not portable to other languages. The
igbinaryextension is a compact alternative.