serialize
Turns a value into a string for storage.
Syntax
serialize(mixed $value): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $value | mixed | Any 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";}}
AnnaNotes
Never call unserialize() on user data: it lets someone run their own code. Use JSON for exchanging data.
See also
- json_encode — Converts a value to a JSON string.
- var_export — Prints a value as PHP code.
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.