serialize

serialize -- Generates a storable representation of a value.

Description

string serialize(mixed value);

serialize() returns a string containing a byte-stream representation of value that can be stored anywhere.

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, use unserialize(). serialize() handles the types integer, double, string, array (multidimensional) and object (object properties will be serialized, but methods are lost).

Example 1. Serialize() example

  1 
  2 // $session_data contains a multi-dimensional array with session
  3 // information for the current user.  We use serialize() to store
  4 // it in a database at the end of the request.
  5 
  6 $conn = odbc_connect ("webdb", "php", "chicken");
  7 $stmt = odbc_prepare ($conn,
  8                       "UPDATE sessions SET data = ? WHERE id = ?");
  9 $sqldata = array (serialize($session_data), $PHP_AUTH_USER);
 10 if (!odbc_execute ($stmt, &$sqldata)) {
 11     $stmt = odbc_prepare($conn,
 12                          "INSERT INTO sessions (id, data) VALUES(?, ?)");
 13     if (!odbc_execute($stmt, &$sqldata)) {
 14         /* Something went wrong.  Bitch, whine and moan. */
 15     }
 16 }
 17