<HTML ><HEAD ><TITLE >serialize</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Variable Functions" HREF="ref.variables.html"><LINK REL="PREVIOUS" TITLE="print_r" HREF="function.print-r.html"><LINK REL="NEXT" TITLE="settype" HREF="function.settype.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.print-r.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.settype.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.serialize" ></A >serialize</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN94358" ></A ><P > (PHP 3>= 3.0.5, PHP 4 )</P >serialize -- Generates a storable representation of a value </DIV ><DIV CLASS="refsect1" ><A NAME="AEN94361" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >serialize</B > ( mixed value)<BR ></BR ><P > <B CLASS="function" >serialize()</B > returns a string containing a byte-stream representation of <TT CLASS="parameter" ><I >value</I ></TT > that can be stored anywhere. </P ><P > This is useful for storing or passing PHP values around without losing their type and structure. </P ><P > To make the serialized string into a PHP value again, use <A HREF="function.unserialize.html" ><B CLASS="function" >unserialize()</B ></A >. <B CLASS="function" >serialize()</B > handles all types, except the <A HREF="language.types.resource.html" ><B CLASS="type" >resource</B ></A >-type. You can even <B CLASS="function" >serialize()</B > arrays that contain references to itself. References inside the array/object you are <B CLASS="function" >serialize()</B >ing will also be stored. </P ><P > When serializing objects, PHP will attempt to call the member function <B CLASS="function" >__sleep()</B > prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using <A HREF="function.unserialize.html" ><B CLASS="function" >unserialize()</B ></A > the <B CLASS="function" >__wakeup()</B > member function is called. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > In PHP 3, object properties will be serialized, but methods are lost. PHP 4 removes that limitation and restores both properties and methods. Please see the <A HREF="language.oop.serialization.html" >Serializing Objects</A > section of <A HREF="language.oop.html" >Classes and Objects</A > for more information. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN94388" ></A ><P ><B >Example 1. <B CLASS="function" >serialize()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// $session_data contains a multi-dimensional array with session // information for the current user. We use serialize() to store // it in a database at the end of the request. $conn = odbc_connect ("webdb", "php", "chicken"); $stmt = odbc_prepare ($conn, "UPDATE sessions SET data = ? WHERE id = ?"); $sqldata = array (serialize($session_data), $PHP_AUTH_USER); if (!odbc_execute ($stmt, &$sqldata)) { $stmt = odbc_prepare($conn, "INSERT INTO sessions (id, data) VALUES(?, ?)"); if (!odbc_execute($stmt, &$sqldata)) { /* Something went wrong. Bitch, whine and moan. */ } }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See Also: <A HREF="function.unserialize.html" ><B CLASS="function" >unserialize()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.print-r.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.settype.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >print_r</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.variables.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >settype</TD ></TR ></TABLE ></DIV ></BODY ></HTML >