Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

unserialize()

Reconstruct a value from trusted PHP serialization data.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Reconstruct a value from trusted PHP serialization data. The encoding b:0; legitimately returns false, which is also the result for malformed input accompanied by a warning. The allowed_classes option restricts object restoration. Even with that option, avoid untrusted serialized input. Since PHP 8.3 trailing bytes after a valid value emit E_WARNING while returning that value.

Signature

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

Example output

["007","int",false]
["__PHP_Incomplete_Class",7,8]

Examples

<?php
$raw=serialize(["sku"=>"007","stock"=>0]);
$restored=unserialize($raw,["allowed_classes"=>false]);
echo json_encode([$restored["sku"],get_debug_type($restored["stock"]),unserialize("b:0;")]);
class Ticket { public int $number=7; } $bytes=serialize(new Ticket());
$blocked=unserialize($bytes,["allowed_classes"=>false]); $allowed=unserialize($bytes,["allowed_classes"=>[Ticket::class]]);
set_error_handler(fn($level)=>$level===E_WARNING); $head=unserialize("i:8;extra"); restore_error_handler();
echo "\n",json_encode([get_class($blocked),$allowed->number,$head]);

Result

["007","int",false]
["__PHP_Incomplete_Class",7,8]

Sources