PHP serialize() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, serialize() converts a PHP value into a string that can be stored or sent somewhere. Later, unserialize() can turn that string back into the original value.

This is handy for saving arrays and objects in a file, a cache or a database column. The result is a PHP-specific text format, so it is not the same as JSON. It works with almost every type, except resources and closures.

Syntax

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

PHP
$text = serialize($value);
CallWhat it does
serialize(42)Returns the string i:42;
serialize('hello')Returns the string s:5:"hello";
serialize(['a' => 1])Returns the string a:1:{s:1:"a";i:1;}

Use json_encode() instead when the data must be shared with JavaScript or other languages. serialize() output is meant to be read back by PHP.

Library declarations
PHP
serialize(mixed $value): string

Parameters

ParameterDescription
valueRequired. The value to serialize. It can be a string, number, boolean, null, array or object. Resources and closures cannot be serialized.

Return Value

Return ValueDescription
stringA string that represents the value. Pass it to unserialize() to get the value back.

PHP Examples

Example 1 — Serializing Simple Values

See what the serialized form looks like for an integer, a string, a boolean, null and a float.

PHP
<?php
echo serialize(42), PHP_EOL;
echo serialize('hello'), PHP_EOL;
echo serialize(true), PHP_EOL;
echo serialize(null), PHP_EOL;
echo serialize(1.5), PHP_EOL;

How It Works

  1. Integers start with i. i:42; means an integer with the value 42.
  2. Strings include their length. s:5:"hello"; says a string of 5 characters follows.
  3. Booleans use b and null uses N. true becomes b:1; and null becomes N;.
  4. Floats start with d. d:1.5; stands for a double, which is PHP's float type.

Example 2 — Arrays and Objects

Nested arrays and objects are written with counts and curly braces. Look at both formats.

PHP
<?php
echo serialize(['a' => 1, 'b' => [true, null]]), PHP_EOL;

class Point {
    public $x = 1;
    public $y = 2;
}

echo serialize(new Point()), PHP_EOL;

How It Works

  1. Arrays start with a and a count. a:2:{...} means an array with two elements.
  2. Each key and value is written one after the other. The nested array repeats the same pattern inside the braces.
  3. Objects start with O and the class name. O:5:"Point" is an object of a class named Point, which has 5 letters.
  4. Properties follow the count. Each property name and value is written in the same style as an array.

Example 3 — Saving and Restoring Data

Serialize a shopping cart, restore it with unserialize(), and confirm that nothing was lost.

PHP
<?php
$cart = ['apple' => 3, 'pear' => 5];

$saved = serialize($cart);
echo $saved, PHP_EOL;

$restored = unserialize($saved, ['allowed_classes' => false]);
var_dump($restored === $cart);
echo $restored['pear'], PHP_EOL;

How It Works

  1. The cart is a normal array. It maps fruit names to quantities.
  2. serialize() turns it into one string. You could now save that string in a file or a database.
  3. unserialize() rebuilds the array. The allowed_classes option is false because we only expect plain data.
  4. The restored array is identical. The === comparison prints true and the pear count is 5.

Frequently Asked Questions

It converts a PHP value, such as an array or object, into a storable string. You can save the string and later use unserialize() to get the original value back.
serialize() uses a PHP-only format and keeps object types and classes. json_encode() makes standard JSON that other languages can read, but it loses class information and only keeps public properties.
No. Closures cannot be serialized and PHP throws an exception, and resources such as file handles are turned into an integer 0 and are not usable after restoring.
No. Never call unserialize() on data that a user can change, because it can create objects and run code in their magic methods. Use json_decode() for untrusted input, or pass allowed_classes => false.
It is used to store sessions, caches, queue jobs and settings. PHP's own session handler uses a similar format to save session data.

Next: settype()

settype() changes the type of a variable in place. You give it a variable and the name of .

settype() 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