Introduction

The unserialize() function is a built-in function in PHP that converts a string representation of a variable that was created with the serialize() function back into a PHP value.

Syntax

The syntax of the unserialize() function is as follows:

mixed unserialize(string $str)

The function takes a single parameter, $str, which is the serialized string to be converted back into a PHP value. The function returns the PHP value represented by the serialized string.

Example Usage

Here is an example of how to use the unserialize() function in PHP:

<?php
$serialized_string = 'a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"cherry";}';
$array = unserialize($serialized_string);
print_r($array);
?>

In this example, we define a serialized string $serialized_string that represents an array containing three elements. We use the unserialize() function to convert the serialized string back into an array. We then use the print_r() function to print the resulting array to the output. The output shows the contents of the array in a human-readable format:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

Conclusion

The unserialize() function is a useful tool for converting a serialized string created with the serialize() function back into a PHP value. It can be used to recreate complex data structures such as arrays and objects. By using this function, developers can ensure that their data is restored to its original form and use it in their code. However, it is important to note that the serialized data may be sensitive and should be stored securely.

Practice Your Knowledge

What is the functionality of PHP's unserialize function?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?