PHP Function Reference

PHP serialize() Function



The PHP serialize() function is used to generate a storable representation of a value. This is useful for storing or passing PHP values around without losing their type and structure.

To make the serialized string into a PHP value again, unserialize() function can be used.

Syntax

serialize(value)

Parameters

value Required. Specify the value to be serialized. This function handles all types, except the resource-type and some objects. It is possible to serialize() arrays that contain references to itself. Circular references inside the array/object that are serialized will also be stored. Any other reference will be lost.

Return Value

Returns a string containing a byte-stream representation of value that can be stored anywhere.

Note: The returned string is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field.

Example: serialize() example

The example below shows the usage of serialize() function.

<?php
//defining an array 
$arr = array( 
    "Hello", 
    array(10, 20),
    "World"
);

//serializing the array 
$str = serialize($arr); 
  
//displaying the result
echo $str; 
?>

The output of the above code will be:

a:3:{i:0;s:5:"Hello";i:1;a:2:{i:0;i:10;i:1;i:20;}i:2;s:5:"World";}

❮ PHP Variable Handling Reference