PHP unserialize() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, unserialize() turns a string created by serialize() back into a PHP value. It can rebuild integers, strings, arrays and even objects.

Security warning: never call unserialize() on data that comes from a user, a cookie or any untrusted place. Crafted data can create objects of any class and trigger their magic methods, which attackers use to run code. For untrusted data, use json_decode(), or at least pass the allowed_classes option.

Syntax

Call unserialize() with the values you need. Start with this line.

PHP
$value = unserialize($data, ['allowed_classes' => false]);
CallWhat it does
unserialize('i:42;')Returns the integer 42. Only use this with data you created yourself.
unserialize($data, ['allowed_classes' => false])Safer. Objects are not created and become __PHP_Incomplete_Class instead.
unserialize('not valid')Returns false and raises a notice or warning because the string is not valid serialized data.

Always pass allowed_classes with false or a short list of class names. Better still, store data as JSON and read it with json_decode(), which cannot create objects of arbitrary classes.

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

Parameters

ParameterDescription
dataRequired. The serialized string, usually created by serialize(). Treat any string from outside your code as untrusted.
optionsOptional. An array of options. allowed_classes can be false (no objects), true (any class, the unsafe default), or an array of class names. max_depth sets how deep nested data may go, and the default is 4096.

Return Value

Return ValueDescription
mixedThe original value. It returns false if the string is invalid, but the valid data b:0; also gives false, so compare with serialize(false) when you need to be sure.

PHP Examples

Example 1 — Restoring an Array

Serialize an array that your own code created, then read it back. This is the safe use of unserialize().

PHP
<?php
$data = serialize(['name' => 'Asha', 'age' => 25]);
echo $data, PHP_EOL;

$user = unserialize($data);
echo $user['name'], PHP_EOL;
echo $user['age'], PHP_EOL;

How It Works

  1. serialize() creates the string. The data comes from our own code, so we can trust it.
  2. echo shows the stored format. It lists each key and value with its type and length.
  3. unserialize() rebuilds the array. $user is a normal PHP array again.
  4. We can read items as usual. $user['name'] gives Asha and $user['age'] gives 25.

Example 2 — Failure Versus a Real false

unserialize() returns false for bad input, but false is also a valid value. Check carefully, and use the @ sign here only to hide the PHP notice for this demo.

PHP
<?php
$input = 'this is not serialized';
$result = @unserialize($input);

if ($result === false && $input !== serialize(false)) {
    echo 'Invalid data', PHP_EOL;
}

var_dump(unserialize('b:0;'));

How It Works

  1. The input is plain text. It is not in the serialized format, so unserialize() fails and returns false.
  2. The @ sign hides the warning. In real code, log the problem instead of hiding it.
  3. Comparing with serialize(false) separates failure from a real false. Our text does not match b:0;, so it is invalid.
  4. The string b:0; is valid. It restores the boolean false, so no error is shown.

Example 3 — Limiting Classes with allowed_classes

Serialize an object, then restore it twice: once with objects blocked, and once with only the Point class allowed.

PHP
<?php
class Point {
    public $x = 1;
    public $y = 2;
}

$data = serialize(new Point());

$safe = unserialize($data, ['allowed_classes' => false]);
echo get_class($safe), PHP_EOL;

$trusted = unserialize($data, ['allowed_classes' => ['Point']]);
echo get_class($trusted), PHP_EOL;
echo $trusted->x + $trusted->y, PHP_EOL;

How It Works

  1. We serialize a Point object. The string records the class name Point.
  2. With allowed_classes set to false, no real object is made. PHP creates a harmless __PHP_Incomplete_Class instead.
  3. With ['Point'], only that class is allowed. The result is a real Point object.
  4. The trusted object works normally. x plus y gives 3.

Frequently Asked Questions

It takes a string made by serialize() and turns it back into a PHP value, such as an array or an object. It returns false if the string is not valid serialized data.
No. Unserializing untrusted data can lead to PHP object injection and even remote code execution. Never pass cookies, form fields or other user data to it. Use json_decode() instead.
It controls which classes unserialize() may create. Set it to false to block all objects, or to an array of class names to allow only those. Blocked objects become __PHP_Incomplete_Class.
unserialize() returns false for both. Compare the input with serialize(false), which is 'b:0;'. If they do not match and the result is false, the data was invalid.
Use JSON for data that crosses a trust boundary. json_encode() and json_decode() only produce plain arrays and objects, so they cannot trigger the magic methods of your classes.

Next: unset()

unset() destroys one or more variables. After you call it, the variable no longer exists, .

unset() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful