Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

serialize

Turns a value into a string for storage.

Syntax

serialize(mixed $value): string

Parameters

ParameterTypeDescription
$valuemixedAny value, including objects.

Return value

stringA string from which the value can be restored.

Examples

Saving and restoring an array:

<?php
 
$data = ['name' => 'Anna', 'tags' => ['php']];
$text = serialize($data);
 
echo $text, "\n";
echo unserialize($text)['name'];

Output:

a:2:{s:4:"name";s:4:"Anna";s:4:"tags";a:1:{i:0;s:3:"php";}}
Anna

Notes

Never call unserialize() on user data: it lets someone run their own code. Use JSON for exchanging data.

See also

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.