PHP unserialize() Function
The PHP unserialize() function is used to convert takes a serialized variable back into a PHP value.
Syntax
unserialize(data, options)
Parameters
data |
Required. Specify the serialized string. |
options |
Optional. Specify options to be provided to the function, as an associative array. Can be either an array of class names which should be accepted, false to accept no classes, or true to accept all classes. Default is true. |
Return Value
Returns the converted value, and it can be a bool, int, float, string, array or object. In case the passed string is not unserializeable, false is returned and E_NOTICE is issued.
Exceptions
Objects may throw Throwables in their unserialization handlers.
Example: unserialize() example
The example below shows the usage of unserialize() function.
<?php //defining an array $arr = array( "Hello", array(10, 20), "World" ); //serializing the array $str = serialize($arr); //displaying the result echo $str."\n"; //unserializing the string $newStr = unserialize($str); //displaying the result print_r($newStr); ?>
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";} Array ( [0] => Hello [1] => Array ( [0] => 10 [1] => 20 ) [2] => World )
❮ PHP Variable Handling Reference