PHP Serialization: Complete Guide to Serialize, Unserialize and Convert Your Data

PHP Serialization: Complete Guide to Serialize, Unserialize and Convert Your Data

Master PHP serialization: serialize, unserialize, JSON↔PHP conversion. Practical guide with concrete examples and security best practices.

13.04.2026
9 min read
Share this article:
php
serialization
unserialize
json
development
security

What is PHP serialization?

PHP serialization turns structures (arrays, objects, scalars) into a textual binary-safe string you can store in a database, write to disk, or move across processes — then <strong>unserialize()</strong> rebuilds the value. This powers native PHP sessions, many app caches, and WordPress-style metadata. To convert or inspect serialized PHP without installing extensions, use our online PHP serialization tool. This guide covers the wire format, how it compares to JSON, why <code>unserialize()</code> on untrusted input is dangerous, and practical notes (WordPress, safer alternatives). For API-style payloads, see our complete JSON minification guide; when you ship compact JS, an online JavaScript minifier helps, and for readability a JavaScript unminifier supports debugging.

Sessions and caches: compact representation native to PHP stacks
Debug serialized blobs from WordPress or legacy exports directly in the browser
Clear JSON vs PHP guidance for public APIs vs internal PHP storage
Explicit coverage of object-injection risks and mitigations
Bidirectional JSON ↔ PHP conversion for frontends and REST backends

Where PHP serialization actually matters

Sessions, caches, and plugins

PHP sessions often persist serialized server-side state; frameworks and CMSs serialize options, job payloads, or object snapshots for later hydration. Understanding the format helps you fix corrupted rows or migrate safely to JSON.

Conceptual illustration of PHP data flows and serialization

Before optimization

File size:Opaque blob
Load time:Hard to debug

After optimization

File size:Structured view
Load time:Fast inspection via tool
Common real-world cases

You will routinely encounter serialized PHP in production environments such as:

WordPress options and metadata (transients, postmeta) often stored serialized
Application caches mapping keys to serialized values to avoid rebuilding heavy objects
Queues or internal logs embedding array snapshots
Interop: exporting a PHP structure to a string before JSON conversion at the API boundary
Format at a glance

Before running SQL find/replace migrations, map nested types and string length segments so you do not break s:n:"…" declarations.

Diagram of PHP serialize types and nesting

WordPress, migrations, and hygiene

WordPress: options, transients, meta

Many meta values are stored serialized. Before bulk SQL replacements (e.g., domain changes), deserialize and reserialize properly — otherwise you break string length prefixes in s:n:"...". Use dedicated tools or PHP scripts, and validate with FastMinify on copies.

Always test on staging with a database snapshot
Prefer WordPress APIs (update_option, metadata helpers) over raw SQL hacks
If you expose a REST API, serialize responses as JSON — not public PHP serialize
Security and maintenance checklist

Align with OWASP guidance: no deserialization of arbitrary attacker-controlled blobs, log anomalies, rotate secrets if cache poisoning was possible.

Sign or encrypt sensitive blobs stored outside server-side sessions
Pair frontend JSON with schema validation; use /en/minify-json for production payload hygiene
Document fields still using PHP serialize to plan their retirement
Train teams: the format is not a security boundary by itself

serialize(), unserialize(), and reading the format

From PHP values to a serialized string

serialize() emits a typed textual representation; unserialize() restores the value. Minimal associative array example:

Before

<?php $user = ['id' => 42, 'name' => 'Ada', 'roles' => ['editor', 'reviewer']]; echo serialize($user);

After

a:3:{s:2:"id";i:42;s:4:"name";s:3:"Ada";s:5:"roles";a:2:{i:0;s:6:"editor";i:1;s:8:"reviewer";}}
Reading type codes safely

Each fragment starts with a type letter and colons. Reading the format helps debugging without executing attacker-controlled payloads.

a:N:{...} : array with N entries (key/value pairs for associative arrays)
i:123 ; d:3.14 ; b:0|1 : integer, float, boolean
s:n:"..." : byte-length n string (mind UTF-8 byte counts)
O:8:"ClassName" : object — this is where careless unserialize() becomes critical
Prefer pasting unknown blobs into an online viewer instead of evaluating them in production

Serialize, unserialize, and convert with FastMinify

Quick workflow in the browser

Paste serialized PHP or JSON, pick the operation (beautify, minify, JSON ↔ PHP). Processing stays client-side — handy for WordPress exports or legacy APIs returning PHP-serialized strings.

FastMinify interface for PHP serialize and conversions
1

Paste the payload

Bring the string from the database (postmeta, option row, cache) or JSON to convert.

2

Choose the action

Serialize, unserialize, or convert to/from JSON for the task at hand.

3

Validate off production

Compare output with application expectations; never unserialize() untrusted input.

Why use an online tool here?

Mixed PHP + frontend teams save time inspecting a blob without bootstrapping a full stack.

Great for one-offs: no Composer/npm required just to read a value
Pairs with the JSON minifier at /en/minify-json when normalizing payloads
Useful in customer support to visually explain metadata issues
Free and privacy-oriented: your pasted data is not sent to a server

JSON, PHP serialize, and security

JSON vs PHP serialize: pick the right default

JSON is universal for HTTP APIs and browsers; PHP serialize is idiomatic for rich internal PHP storage. Prefer JSON at network boundaries; serialize can remain valid for trusted PHP-only storage.

Basic example

<?php $data = ['ok' => true, 'count' => 3]; $json = json_encode($data, JSON_THROW_ON_ERROR); // {"ok":true,"count":3}

Configuration

<?php $same = ['ok' => true, 'count' => 3]; $php = serialize($same); // a:2:{s:2:"ok";b:1;s:5:"count";i:3;}

Usage

// Interop: json_decode for API input; unserialize ONLY on signed/controlled data.
unserialize() on untrusted data

Never unserialize() raw user input: object injection and magic-method gadget chains are a real risk. Prefer JSON + schema validation, signed formats (HMAC), or strict class allowlists when you must deserialize.

Basic example

<?php // Dangerous — do not do this on HTTP input // $obj = unserialize($_POST['payload']);

Usage

<?php $payload = $_POST['json'] ?? '{}'; $safe = json_decode($payload, true, 512, JSON_THROW_ON_ERROR); // Associative array: no arbitrary object instantiation
Storage-side alternatives

For large binary-efficient storage, igbinary or msgpack shrink payloads while staying inside infrastructure you control — complementary to public JSON APIs.

Configuration

// Conceptual (requires igbinary extension) // $packed = igbinary_serialize($data);

Usage

// Rollout: migrate legacy blobs to JSON + schema validation incrementally

Conclusion

PHP serialization remains core to internal storage and ecosystems like WordPress, but it demands strict discipline around unserialize(). For quick debugging and JSON conversion, a reliable online tool accelerates triage without touching production. Keep JSON at network edges, PHP serialize in trust zones, and measure impact before any migration.

Serialize, unserialize, or convert your PHP data in a few clicks

Replay conversions on copied data before rollout
Cover critical paths with tests asserting json_encode/json_decode behavior
Cross-link the JSON guide and related minify tools from other articles
Watch security advisories for object gadget chains in dependencies
Share this article
Share this article:
PHP Serialization: Complete Guide to Serialize, Unserialize and Convert Your Data