PHP Manual
Prev |  | Next

# unserialize

unserialize -- Creates a PHP value from a stored representation.

## Description

`mixed unserialize`(string str);

unserialize() takes a single serialized variable (see serialize()) and converts it back into a PHP value. The converted value is returned, and can be an integer, double, string, array or object. If an object was serialized, its methods are not preserved in the returned value.

Example 1. Unserialize() example
      1
      2 // Here, we use unserialize() to load session data from a database
      3 // into $session_data.  This example complements the one described
      4 // with serialize().
      5
      6 $conn = odbc_connect ("webdb", "php", "chicken");
      7 $stmt = odbc_prepare ($conn, "SELECT data FROM sessions WHERE id = ?");
      8 $sqldata = array ($PHP_AUTH_USER);
      9 if (!odbc_execute ($stmt, &$sqldata) || !odbc_fetch_into ($stmt, &$tmp)) {
     10     // if the execute or fetch fails, initialize to empty array
     11     $session_data = array();
     12 } else {
     13     // we should now have the serialized data in $tmp[0].
     14     $session_data = unserialize ($tmp[0]);
     15     if (!is_array ($session_data)) {
     16         // something went wrong, initialize to empty array
     17         $session_data = array();
     18     }
     19 }
     20

Prev | Home | Next
unpack | Up | usleep