Read PHP Scalars in serialize() Output and Check Array and Object Round Trips
Read PHP serialized integers and strings, verify array and object round trips, and preserve serialized data as a binary string.
For supported values, PHP’s serialize() turns a value into a string that unserialize() can restore with its type and structure. If you are inspecting a saved value, start with its type marker, but check the restored value too: readable output alone does not prove that an array or object came back in the state you expected. The PHP manual describes serialization as a way to store or pass values without losing their type and structure.
Start with an integer and a string
This standalone example serializes two values, prints their representations, and checks both the values and their restored types:
<?php
$integerData = serialize(42);
$stringData = serialize('Hello');
echo $integerData, PHP_EOL; // i:42;
echo $stringData, PHP_EOL; // s:5:"Hello";
$restoredInteger = unserialize($integerData);
$restoredString = unserialize($stringData);
var_dump($restoredInteger === 42, is_int($restoredInteger));
var_dump($restoredString === 'Hello', is_string($restoredString));
In i:42;, i identifies an integer and 42 is its value. There is no string-style length field. In s:5:"Hello";, s identifies a string and 5 gives its length. Those markers matter when a number and text that looks like a number must remain different PHP types. The comparisons in the example check the restored values, while is_int() and is_string() make the intended types explicit. The format walkthrough describes the i marker and the string length field.
Inspect an array, then compare its structure
Let PHP generate the serialized string for the exact array you care about, then compare the restored result with the original:
<?php
$items = [42, 'Hello'];
$arrayData = serialize($items);
echo $arrayData, PHP_EOL;
$restoredItems = unserialize($arrayData);
var_dump(is_array($restoredItems));
var_dump($restoredItems === $items);
The printed string is the representation PHP generated for this array. The round-trip check does not require interpreting its array-format syntax: is_array() checks the restored type, and the final comparison checks the result against $items. If you change an entry, serialize again rather than assuming the old string still describes the new array.
Read an ordinary object’s O format
Ordinary object serialization uses the O format. Since PHP 7.4, a class can provide __serialize() to choose a data array and __unserialize() to restore its state. The custom object serialization RFC specifies that this array is stored in the O format as if its entries represented object properties. That explains why a hook does not require a separate format for the data it returns.
This standalone PHP 7.4-or-later example makes the saved state explicit:
<?php
class Parcel
{
private $id;
private $label;
public function __construct($id, $label)
{
$this->id = $id;
$this->label = $label;
}
public function __serialize(): array
{
return ['id' => $this->id, 'label' => $this->label];
}
public function __unserialize(array $data): void
{
$this->id = $data['id'];
$this->label = $data['label'];
}
public function state(): array
{
return ['id' => $this->id, 'label' => $this->label];
}
}
$original = new Parcel(42, 'Hello');
$objectData = serialize($original);
echo $objectData, PHP_EOL;
$restored = unserialize($objectData);
var_dump($restored instanceof Parcel);
var_dump($restored instanceof Parcel && $restored->state() === $original->state());
Printing $objectData exposes the object representation; comparing state() checks the part this class chose to save. The hook’s returned array is serialization data, not a requirement to expose those names as public properties. If a class defines both __serialize() and the older __sleep(), the RFC gives __serialize() precedence; the corresponding restoration preference is __unserialize() over __wakeup().
The legacy Serializable mechanism uses a C format with an arbitrary string payload. There is an important exception to treating Serializable classes as C-format producers: on versions supporting the new hooks, a class that implements both mechanisms uses __serialize() for new serialization, producing O format; the older mechanism can still decode existing C payloads. The phase-out RFC specifies a PHP 8.1 deprecation warning for a non-abstract class implementing Serializable without both __serialize() and __unserialize(). Its proposal for PHP 9.0 to reject C payloads is an RFC plan, not a claim about shipped PHP 9.0 behavior. Enumerations are another exception: the PHP manual documents an E code that identifies an enum case.
Preserve the whole binary string
A serialized result is binary data and may contain null bytes. When saving or transferring it, choose a path that preserves every byte; do not treat a successful printout as evidence that the complete value survived. This example checks a string containing a null byte without printing its serialized form as ordinary text:
<?php
$original = "A\0B";
$binaryData = serialize($original);
$restored = unserialize($binaryData);
var_dump(is_string($restored));
var_dump($restored === $original);
var_dump(strpos($restored, "\0") !== false);
Finally, a round trip is not a promise that every PHP value or reference relationship can be preserved. The PHP manual excludes resources and some objects. It says circular references within a serialized array or object can be stored, while other references are lost. For a value with references, check the restored relationship you actually need, not just whether unserialize() returned a value.
