Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

unserialize

Restores a value from a string produced by `serialize()`.

Syntax

unserialize(string $data, array $options = []): mixed

Parameters

ParameterTypeDescription
$datastringThe serialized string.
$optionsarrayOptional. `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

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.

Thanks for reading!

Donate to the site — we work hard for you.