unserialize
Restores a value from a string produced by `serialize()`.
Syntax
unserialize(string $data, array $options = []): mixedParameters
| Parameter | Type | Description |
|---|---|---|
| $data | string | The serialized string. |
| $options | array | Optional. `allowed_classes` — which classes may be created. |
Return value
mixedThe restored value, or `false` on an error.
Examples
An array there and back:
<?php
$text = serialize(['id' => 7, 'tags' => ['php', 'sql']]);
echo $text, "\n";
echo json_encode(unserialize($text, ['allowed_classes' => false]));Output:
a:2:{s:2:"id";i:7;s:4:"tags";a:2:{i:0;s:3:"php";i:1;s:3:"sql";}}
{"id":7,"tags":["php","sql"]}Notes
Never pass visitor data to the function: a crafted string can execute someone else's code. Use JSON to exchange data.
See also
- serialize — Turns a value into a string for storage.
- json_decode — Parses a JSON string.
- var_export — Prints a value as PHP code.
In the tutorial
- The JSON format — JSON in PHP — json_encode and json_decode, non-ASCII text, error handling and storing data in a file.