🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 91 (from laksa176)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
8 hours ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.php.net/manual/en/function.serialize.php
Last Crawled2026-04-06 15:51:49 (8 hours ago)
First Indexed2015-10-21 16:47:00 (10 years ago)
HTTP Status Code200
Meta TitlePHP: serialize - Manual
Meta DescriptionGenerates a storable representation of a value
Meta Canonicalnull
Boilerpipe Text
(PHP 4, PHP 5, PHP 7, PHP 8) serialize — Generates a storable representation of a value Description 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() . Parameters value The value to be serialized. serialize() handles all types, except the resource -type and some object s (see note below). You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost. When serializing objects, PHP will attempt to call the member functions __serialize() or __sleep() 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 unserialize() the __unserialize() or __wakeup() member function is called. Note : Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side. Return Values Returns a string containing a byte-stream representation of value that can be stored anywhere. Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. Examples Example #1 serialize() example <?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 ), $_SERVER [ 'PHP_AUTH_USER' ]); if (! odbc_execute ( $stmt , $sqldata )) { $stmt = odbc_prepare ( $conn , "INSERT INTO sessions (id, data) VALUES(?, ?)" ); if (! odbc_execute ( $stmt , array_reverse ( $sqldata ))) { /* Something went wrong.. */ } } ?> Notes Note : Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable interface or the magic __serialize() / __unserialize() or __sleep() / __wakeup() methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized. There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods. Warning When serialize() serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility. See Also unserialize() - Creates a PHP value from a stored representation var_export() - Outputs or returns a parsable string representation of a variable json_encode() - Returns the JSON representation of a value Serializing Objects __sleep() __wakeup() __serialize() __unserialize() 373 egingell at sisna dot com ¶ 19 years ago <? /* Anatomy of a serialize()'ed value: String s:size:value; Integer i:value; Boolean b:value; (does not store "true" or "false", does store '1' or '0') Null N; Array a:size:{key definition;value definition;(repeated per element)} Object O:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)} String values are always in double quotes Array keys are always integers or strings "null => 'value'" equates to 's:0:"";s:5:"value";', "true => 'value'" equates to 'i:1;s:5:"value";', "false => 'value'" equates to 'i:0;s:5:"value";', "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";', and attempting to use an object as a key will result in the same behavior as using an array will. */ ?> 285 Anonymous ¶ 14 years ago Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries. If you want your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query and modify data in the database without using a third party intermediary tool to manipulate data to be inserted. I've encountered this too many times in my career, it makes for difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized. That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare. 6 mark at bvits dot co dot uk ¶ 3 years ago There is a type not mentioned in the user notes so far, 'E'. This is the newer Enum class that can be utilised: login_security|E:25:"Permission:manageClient" 22 MC_Gurk at gmx dot net ¶ 20 years ago If you are going to serialie an object which contains references to other objects you want to serialize some time later, these references will be lost when the object is unserialized. The references can only be kept if all of your objects are serialized at once. That means: $a = new ClassA(); $b = new ClassB($a); //$b containes a reference to $a; $s1=serialize($a); $s2=serialize($b); $a=unserialize($s1); $b=unserialize($s2); now b references to an object of ClassA which is not $a. $a is another object of Class A. use this: $buf[0]=$a; $buf[1]=$b; $s=serialize($buf); $buf=unserialize($s); $a=$buf[0]; $b=$buf[1]; all references are intact. 24 nh at ngin dot de ¶ 13 years ago Serializing floating point numbers leads to weird precision offset errors: <?php echo round ( 96.670000000000002 , 2 ); // 96.67 echo serialize ( round ( 96.670000000000002 , 2 )); // d:96.670000000000002; echo serialize ( 96.67 ); // d:96.670000000000002; ?> Not only is this wrong, but it adds a lot of unnecessary bulk to serialized data. Probably better to use json_encode() instead (which apparently is faster than serialize(), anyway). 12 Andrew B ¶ 13 years ago When you serialize an array the internal pointer will not be preserved. Apparently this is the expected behavior but was a bit of a gotcha moment for me. Copy and paste example below. <?php //Internal Pointer will be 2 once variables have been assigned. $array = array(); $array [] = 1 ; $array [] = 2 ; $array [] = 3 ; //Unset variables. Internal pointer will still be at 2. unset( $array [ 0 ]); unset( $array [ 1 ]); unset( $array [ 2 ]); //Serialize $serializeArray = serialize ( $array ); //Unserialize $array = unserialize ( $serializeArray ); //Add a new element to the array //If the internal pointer was preserved, the new array key should be 3. //Instead the internal pointer has been reset, and the new array key is 0. $array [] = 4 ; //Expected Key - 3 //Actual Key - 0 echo "<pre>" , print_r ( $array , 1 ) , "</pre>" ; ?> 12 frost at easycast dot ru ¶ 12 years ago Closures cannot be serialized: <?php $func = function () {echo 'hello!' ;}; $func (); // prints "hello!" $result = serialize ( $func ); // Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' ?>
Markdown
[update page now](https://www.php.net/x-myracloud-5958a2bbbed300a9b9ac631223924e0b/1775490709.838) [![](https://www.php.net/images/logos/php-logo-white.svg)](https://www.php.net/) - [Downloads](https://www.php.net/downloads.php) - [Documentation](https://www.php.net/docs.php) - [Get Involved](https://www.php.net/get-involved.php) - [Help](https://www.php.net/support.php) - [![PHP 8.5](https://www.php.net/images/php8/logo_php8_5.svg)](https://www.php.net/releases/8.5/index.php) Search docs [Getting Started](https://www.php.net/manual/en/getting-started.php) [Introduction](https://www.php.net/manual/en/introduction.php) [A simple tutorial](https://www.php.net/manual/en/tutorial.php) [Language Reference](https://www.php.net/manual/en/langref.php) [Basic syntax](https://www.php.net/manual/en/language.basic-syntax.php) [Types](https://www.php.net/manual/en/language.types.php) [Variables](https://www.php.net/manual/en/language.variables.php) [Constants](https://www.php.net/manual/en/language.constants.php) [Expressions](https://www.php.net/manual/en/language.expressions.php) [Operators](https://www.php.net/manual/en/language.operators.php) [Control Structures](https://www.php.net/manual/en/language.control-structures.php) [Functions](https://www.php.net/manual/en/language.functions.php) [Classes and Objects](https://www.php.net/manual/en/language.oop5.php) [Namespaces](https://www.php.net/manual/en/language.namespaces.php) [Enumerations](https://www.php.net/manual/en/language.enumerations.php) [Errors](https://www.php.net/manual/en/language.errors.php) [Exceptions](https://www.php.net/manual/en/language.exceptions.php) [Fibers](https://www.php.net/manual/en/language.fibers.php) [Generators](https://www.php.net/manual/en/language.generators.php) [Attributes](https://www.php.net/manual/en/language.attributes.php) [References Explained](https://www.php.net/manual/en/language.references.php) [Predefined Variables](https://www.php.net/manual/en/reserved.variables.php) [Predefined Exceptions](https://www.php.net/manual/en/reserved.exceptions.php) [Predefined Interfaces and Classes](https://www.php.net/manual/en/reserved.interfaces.php) [Predefined Attributes](https://www.php.net/manual/en/reserved.attributes.php) [Context options and parameters](https://www.php.net/manual/en/context.php) [Supported Protocols and Wrappers](https://www.php.net/manual/en/wrappers.php) [Security](https://www.php.net/manual/en/security.php) [Introduction](https://www.php.net/manual/en/security.intro.php) [General considerations](https://www.php.net/manual/en/security.general.php) [Installed as CGI binary](https://www.php.net/manual/en/security.cgi-bin.php) [Installed as an Apache module](https://www.php.net/manual/en/security.apache.php) [Session Security](https://www.php.net/manual/en/security.sessions.php) [Filesystem Security](https://www.php.net/manual/en/security.filesystem.php) [Database Security](https://www.php.net/manual/en/security.database.php) [Error Reporting](https://www.php.net/manual/en/security.errors.php) [User Submitted Data](https://www.php.net/manual/en/security.variables.php) [Hiding PHP](https://www.php.net/manual/en/security.hiding.php) [Keeping Current](https://www.php.net/manual/en/security.current.php) [Features](https://www.php.net/manual/en/features.php) [HTTP authentication with PHP](https://www.php.net/manual/en/features.http-auth.php) [Cookies](https://www.php.net/manual/en/features.cookies.php) [Sessions](https://www.php.net/manual/en/features.sessions.php) [Handling file uploads](https://www.php.net/manual/en/features.file-upload.php) [Using remote files](https://www.php.net/manual/en/features.remote-files.php) [Connection handling](https://www.php.net/manual/en/features.connection-handling.php) [Persistent Database Connections](https://www.php.net/manual/en/features.persistent-connections.php) [Command line usage](https://www.php.net/manual/en/features.commandline.php) [Garbage Collection](https://www.php.net/manual/en/features.gc.php) [DTrace Dynamic Tracing](https://www.php.net/manual/en/features.dtrace.php) [Function Reference](https://www.php.net/manual/en/funcref.php) [Affecting PHP's Behaviour](https://www.php.net/manual/en/refs.basic.php.php) [Audio Formats Manipulation](https://www.php.net/manual/en/refs.utilspec.audio.php) [Authentication Services](https://www.php.net/manual/en/refs.remote.auth.php) [Command Line Specific Extensions](https://www.php.net/manual/en/refs.utilspec.cmdline.php) [Compression and Archive Extensions](https://www.php.net/manual/en/refs.compression.php) [Cryptography Extensions](https://www.php.net/manual/en/refs.crypto.php) [Database Extensions](https://www.php.net/manual/en/refs.database.php) [Date and Time Related Extensions](https://www.php.net/manual/en/refs.calendar.php) [File System Related Extensions](https://www.php.net/manual/en/refs.fileprocess.file.php) [Human Language and Character Encoding Support](https://www.php.net/manual/en/refs.international.php) [Image Processing and Generation](https://www.php.net/manual/en/refs.utilspec.image.php) [Mail Related Extensions](https://www.php.net/manual/en/refs.remote.mail.php) [Mathematical Extensions](https://www.php.net/manual/en/refs.math.php) [Non-Text MIME Output](https://www.php.net/manual/en/refs.utilspec.nontext.php) [Process Control Extensions](https://www.php.net/manual/en/refs.fileprocess.process.php) [Other Basic Extensions](https://www.php.net/manual/en/refs.basic.other.php) [Other Services](https://www.php.net/manual/en/refs.remote.other.php) [Search Engine Extensions](https://www.php.net/manual/en/refs.search.php) [Server Specific Extensions](https://www.php.net/manual/en/refs.utilspec.server.php) [Session Extensions](https://www.php.net/manual/en/refs.basic.session.php) [Text Processing](https://www.php.net/manual/en/refs.basic.text.php) [Variable and Type Related Extensions](https://www.php.net/manual/en/refs.basic.vartype.php) [Web Services](https://www.php.net/manual/en/refs.webservice.php) [Windows Only Extensions](https://www.php.net/manual/en/refs.utilspec.windows.php) [XML Manipulation](https://www.php.net/manual/en/refs.xml.php) [GUI Extensions](https://www.php.net/manual/en/refs.ui.php) Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search (current page) / Focus search box [settype »](https://www.php.net/manual/en/function.settype.php) [« print\_r](https://www.php.net/manual/en/function.print-r.php) - [Preface](https://www.php.net/manual/en/index.php) - [Function Reference](https://www.php.net/manual/en/funcref.php) - [Variable and Type Related Extensions](https://www.php.net/manual/en/refs.basic.vartype.php) - [Variable handling](https://www.php.net/manual/en/book.var.php) - [Variable handling Functions](https://www.php.net/manual/en/ref.var.php) # serialize (PHP 4, PHP 5, PHP 7, PHP 8) serialize — Generates a storable representation of a value ### Description **serialize**([mixed](https://www.php.net/manual/en/language.types.mixed.php) `$value`): [string](https://www.php.net/manual/en/language.types.string.php) Generates a storable representation of a value. 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()](https://www.php.net/manual/en/function.unserialize.php). ### Parameters `value` The value to be serialized. **serialize()** handles all types, except the [resource](https://www.php.net/manual/en/language.types.resource.php)\-type and some [object](https://www.php.net/manual/en/language.types.object.php)s (see note below). You can even **serialize()** arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost. When serializing objects, PHP will attempt to call the member functions [\_\_serialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.serialize) or [\_\_sleep()](https://www.php.net/manual/en/language.oop5.magic.php#object.sleep) 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 [unserialize()](https://www.php.net/manual/en/function.unserialize.php) the [\_\_unserialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize) or [\_\_wakeup()](https://www.php.net/manual/en/language.oop5.magic.php#object.wakeup) member function is called. > **Note**: > > Object's private members have the class name prepended to the member name; protected members have a '\*' prepended to the member name. These prepended values have null bytes on either side. ### Return Values Returns a string containing a byte-stream representation of `value` that can be stored anywhere. Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, **serialize()** output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. ### Examples **Example \#1 **serialize()** example** ``` <?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), $_SERVER['PHP_AUTH_USER']); if (!odbc_execute($stmt, $sqldata)) { $stmt = odbc_prepare($conn, "INSERT INTO sessions (id, data) VALUES(?, ?)"); if (!odbc_execute($stmt, array_reverse($sqldata))) { /* Something went wrong.. */ } } ?> ``` ### Notes > **Note**: > > Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the [Serializable](https://www.php.net/manual/en/class.serializable.php) interface or the magic [\_\_serialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.serialize)/[\_\_unserialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize) or [\_\_sleep()](https://www.php.net/manual/en/language.oop5.magic.php#object.sleep)/[\_\_wakeup()](https://www.php.net/manual/en/language.oop5.magic.php#object.wakeup) methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized. > > There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods. **Warning** When **serialize()** serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility. ### See Also - [unserialize()](https://www.php.net/manual/en/function.unserialize.php) - Creates a PHP value from a stored representation - [var\_export()](https://www.php.net/manual/en/function.var-export.php) - Outputs or returns a parsable string representation of a variable - [json\_encode()](https://www.php.net/manual/en/function.json-encode.php) - Returns the JSON representation of a value - [Serializing Objects](https://www.php.net/manual/en/language.oop5.serialization.php) - [\_\_sleep()](https://www.php.net/manual/en/language.oop5.magic.php#object.sleep) - [\_\_wakeup()](https://www.php.net/manual/en/language.oop5.magic.php#object.wakeup) - [\_\_serialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.serialize) - [\_\_unserialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize) ### Found A Problem? [Learn How To Improve This Page](https://github.com/php/doc-base/blob/master/README.md "This will take you to our contribution guidelines on GitHub") • [Submit a Pull Request](https://github.com/php/doc-en/blob/master/reference/var/functions/serialize.xml) • [Report a Bug](https://github.com/php/doc-en/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2Ffunction.serialize%0A%0A---) [+add a note](https://www.php.net/manual/add-note.php?sect=function.serialize&repo=en&redirect=https://www.php.net/manual/en/function.serialize.php) ### User Contributed Notes 7 notes [up](https://www.php.net/manual/vote-note.php?id=66147&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=66147&page=function.serialize&vote=down "Vote down!") 373 [***egingell at sisna dot com***](https://www.php.net/manual/en/function.serialize.php#66147) [¶](https://www.php.net/manual/en/function.serialize.php#66147) **19 years ago** ``` <? /* Anatomy of a serialize()'ed value: String s:size:value; Integer i:value; Boolean b:value; (does not store "true" or "false", does store '1' or '0') Null N; Array a:size:{key definition;value definition;(repeated per element)} Object O:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)} String values are always in double quotes Array keys are always integers or strings "null => 'value'" equates to 's:0:"";s:5:"value";', "true => 'value'" equates to 'i:1;s:5:"value";', "false => 'value'" equates to 'i:0;s:5:"value";', "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";', and attempting to use an object as a key will result in the same behavior as using an array will. */ ?> ``` [up](https://www.php.net/manual/vote-note.php?id=107717&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=107717&page=function.serialize&vote=down "Vote down!") 285 [***Anonymous***](https://www.php.net/manual/en/function.serialize.php#107717) [¶](https://www.php.net/manual/en/function.serialize.php#107717) **14 years ago** ``` Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries. If you want your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query and modify data in the database without using a third party intermediary tool to manipulate data to be inserted. I've encountered this too many times in my career, it makes for difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized. That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare. ``` [up](https://www.php.net/manual/vote-note.php?id=128200&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=128200&page=function.serialize&vote=down "Vote down!") 6 [***mark at bvits dot co dot uk***](https://www.php.net/manual/en/function.serialize.php#128200) [¶](https://www.php.net/manual/en/function.serialize.php#128200) **3 years ago** ``` There is a type not mentioned in the user notes so far, 'E'. This is the newer Enum class that can be utilised: login_security|E:25:"Permission:manageClient" ``` [up](https://www.php.net/manual/vote-note.php?id=60317&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=60317&page=function.serialize&vote=down "Vote down!") 22 [***MC\_Gurk at gmx dot net***](https://www.php.net/manual/en/function.serialize.php#60317) [¶](https://www.php.net/manual/en/function.serialize.php#60317) **20 years ago** ``` If you are going to serialie an object which contains references to other objects you want to serialize some time later, these references will be lost when the object is unserialized. The references can only be kept if all of your objects are serialized at once. That means: $a = new ClassA(); $b = new ClassB($a); //$b containes a reference to $a; $s1=serialize($a); $s2=serialize($b); $a=unserialize($s1); $b=unserialize($s2); now b references to an object of ClassA which is not $a. $a is another object of Class A. use this: $buf[0]=$a; $buf[1]=$b; $s=serialize($buf); $buf=unserialize($s); $a=$buf[0]; $b=$buf[1]; all references are intact. ``` [up](https://www.php.net/manual/vote-note.php?id=111168&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=111168&page=function.serialize&vote=down "Vote down!") 24 [***nh at ngin dot de***](https://www.php.net/manual/en/function.serialize.php#111168) [¶](https://www.php.net/manual/en/function.serialize.php#111168) **13 years ago** ``` Serializing floating point numbers leads to weird precision offset errors: <?php echo round(96.670000000000002, 2); // 96.67 echo serialize(round(96.670000000000002, 2)); // d:96.670000000000002; echo serialize(96.67); // d:96.670000000000002; ?> Not only is this wrong, but it adds a lot of unnecessary bulk to serialized data. Probably better to use json_encode() instead (which apparently is faster than serialize(), anyway). ``` [up](https://www.php.net/manual/vote-note.php?id=109953&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=109953&page=function.serialize&vote=down "Vote down!") 12 [***Andrew B***](https://www.php.net/manual/en/function.serialize.php#109953) [¶](https://www.php.net/manual/en/function.serialize.php#109953) **13 years ago** ``` When you serialize an array the internal pointer will not be preserved. Apparently this is the expected behavior but was a bit of a gotcha moment for me. Copy and paste example below. <?php //Internal Pointer will be 2 once variables have been assigned. $array = array(); $array[] = 1; $array[] = 2; $array[] = 3; //Unset variables. Internal pointer will still be at 2. unset($array[0]); unset($array[1]); unset($array[2]); //Serialize $serializeArray = serialize($array); //Unserialize $array = unserialize($serializeArray); //Add a new element to the array //If the internal pointer was preserved, the new array key should be 3. //Instead the internal pointer has been reset, and the new array key is 0. $array[] = 4; //Expected Key - 3 //Actual Key - 0 echo "<pre>" , print_r($array, 1) , "</pre>"; ?> ``` [up](https://www.php.net/manual/vote-note.php?id=113305&page=function.serialize&vote=up "Vote up!") [down](https://www.php.net/manual/vote-note.php?id=113305&page=function.serialize&vote=down "Vote down!") 12 [***frost at easycast dot ru***](https://www.php.net/manual/en/function.serialize.php#113305) [¶](https://www.php.net/manual/en/function.serialize.php#113305) **12 years ago** ``` Closures cannot be serialized: <?php $func = function () {echo 'hello!';}; $func(); // prints "hello!" $result = serialize($func); // Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' ?> ``` [+add a note](https://www.php.net/manual/add-note.php?sect=function.serialize&repo=en&redirect=https://www.php.net/manual/en/function.serialize.php) - [Variable handling Functions](https://www.php.net/manual/en/ref.var.php) - [boolval](https://www.php.net/manual/en/function.boolval.php "boolval") - [debug\_​zval\_​dump](https://www.php.net/manual/en/function.debug-zval-dump.php "debug_​zval_​dump") - [doubleval](https://www.php.net/manual/en/function.doubleval.php "doubleval") - [empty](https://www.php.net/manual/en/function.empty.php "empty") - [floatval](https://www.php.net/manual/en/function.floatval.php "floatval") - [get\_​debug\_​type](https://www.php.net/manual/en/function.get-debug-type.php "get_​debug_​type") - [get\_​defined\_​vars](https://www.php.net/manual/en/function.get-defined-vars.php "get_​defined_​vars") - [get\_​resource\_​id](https://www.php.net/manual/en/function.get-resource-id.php "get_​resource_​id") - [get\_​resource\_​type](https://www.php.net/manual/en/function.get-resource-type.php "get_​resource_​type") - [gettype](https://www.php.net/manual/en/function.gettype.php "gettype") - [intval](https://www.php.net/manual/en/function.intval.php "intval") - [is\_​array](https://www.php.net/manual/en/function.is-array.php "is_​array") - [is\_​bool](https://www.php.net/manual/en/function.is-bool.php "is_​bool") - [is\_​callable](https://www.php.net/manual/en/function.is-callable.php "is_​callable") - [is\_​countable](https://www.php.net/manual/en/function.is-countable.php "is_​countable") - [is\_​double](https://www.php.net/manual/en/function.is-double.php "is_​double") - [is\_​float](https://www.php.net/manual/en/function.is-float.php "is_​float") - [is\_​int](https://www.php.net/manual/en/function.is-int.php "is_​int") - [is\_​integer](https://www.php.net/manual/en/function.is-integer.php "is_​integer") - [is\_​iterable](https://www.php.net/manual/en/function.is-iterable.php "is_​iterable") - [is\_​long](https://www.php.net/manual/en/function.is-long.php "is_​long") - [is\_​null](https://www.php.net/manual/en/function.is-null.php "is_​null") - [is\_​numeric](https://www.php.net/manual/en/function.is-numeric.php "is_​numeric") - [is\_​object](https://www.php.net/manual/en/function.is-object.php "is_​object") - [is\_​real](https://www.php.net/manual/en/function.is-real.php "is_​real") - [is\_​resource](https://www.php.net/manual/en/function.is-resource.php "is_​resource") - [is\_​scalar](https://www.php.net/manual/en/function.is-scalar.php "is_​scalar") - [is\_​string](https://www.php.net/manual/en/function.is-string.php "is_​string") - [isset](https://www.php.net/manual/en/function.isset.php "isset") - [print\_​r](https://www.php.net/manual/en/function.print-r.php "print_​r") - [serialize](https://www.php.net/manual/en/function.serialize.php "serialize") - [settype](https://www.php.net/manual/en/function.settype.php "settype") - [strval](https://www.php.net/manual/en/function.strval.php "strval") - [unserialize](https://www.php.net/manual/en/function.unserialize.php "unserialize") - [unset](https://www.php.net/manual/en/function.unset.php "unset") - [var\_​dump](https://www.php.net/manual/en/function.var-dump.php "var_​dump") - [var\_​export](https://www.php.net/manual/en/function.var-export.php "var_​export") - [Copyright © 2001-2026 The PHP Documentation Group](https://www.php.net/manual/en/copyright.php) - [My PHP.net](https://www.php.net/my.php) - [Contact](https://www.php.net/contact.php) - [Other PHP.net sites](https://www.php.net/sites.php) - [Privacy policy](https://www.php.net/privacy.php) [![To Top](https://www.php.net/images/to-top@2x.png)]() `↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open Press `Enter` without selection to search using Google
Readable Markdown
(PHP 4, PHP 5, PHP 7, PHP 8) serialize — Generates a storable representation of a value ### Description 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()](https://www.php.net/manual/en/function.unserialize.php). ### Parameters `value` The value to be serialized. **serialize()** handles all types, except the [resource](https://www.php.net/manual/en/language.types.resource.php)\-type and some [object](https://www.php.net/manual/en/language.types.object.php)s (see note below). You can even **serialize()** arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost. When serializing objects, PHP will attempt to call the member functions [\_\_serialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.serialize) or [\_\_sleep()](https://www.php.net/manual/en/language.oop5.magic.php#object.sleep) 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 [unserialize()](https://www.php.net/manual/en/function.unserialize.php) the [\_\_unserialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize) or [\_\_wakeup()](https://www.php.net/manual/en/language.oop5.magic.php#object.wakeup) member function is called. > **Note**: > > Object's private members have the class name prepended to the member name; protected members have a '\*' prepended to the member name. These prepended values have null bytes on either side. ### Return Values Returns a string containing a byte-stream representation of `value` that can be stored anywhere. Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, **serialize()** output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. ### Examples **Example \#1 **serialize()** example** ### Notes > **Note**: > > Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the [Serializable](https://www.php.net/manual/en/class.serializable.php) interface or the magic [\_\_serialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.serialize)/[\_\_unserialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize) or [\_\_sleep()](https://www.php.net/manual/en/language.oop5.magic.php#object.sleep)/[\_\_wakeup()](https://www.php.net/manual/en/language.oop5.magic.php#object.wakeup) methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized. > > There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods. **Warning** When **serialize()** serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility. ### See Also - [unserialize()](https://www.php.net/manual/en/function.unserialize.php) - Creates a PHP value from a stored representation - [var\_export()](https://www.php.net/manual/en/function.var-export.php) - Outputs or returns a parsable string representation of a variable - [json\_encode()](https://www.php.net/manual/en/function.json-encode.php) - Returns the JSON representation of a value - [Serializing Objects](https://www.php.net/manual/en/language.oop5.serialization.php) - [\_\_sleep()](https://www.php.net/manual/en/language.oop5.magic.php#object.sleep) - [\_\_wakeup()](https://www.php.net/manual/en/language.oop5.magic.php#object.wakeup) - [\_\_serialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.serialize) - [\_\_unserialize()](https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize) 373 [***egingell at sisna dot com***](https://www.php.net/manual/en/function.serialize.php#66147) [¶](https://www.php.net/manual/en/function.serialize.php#66147) **19 years ago** ``` <? /* Anatomy of a serialize()'ed value: String s:size:value; Integer i:value; Boolean b:value; (does not store "true" or "false", does store '1' or '0') Null N; Array a:size:{key definition;value definition;(repeated per element)} Object O:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)} String values are always in double quotes Array keys are always integers or strings "null => 'value'" equates to 's:0:"";s:5:"value";', "true => 'value'" equates to 'i:1;s:5:"value";', "false => 'value'" equates to 'i:0;s:5:"value";', "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";', and attempting to use an object as a key will result in the same behavior as using an array will. */ ?> ``` 285 [***Anonymous***](https://www.php.net/manual/en/function.serialize.php#107717) [¶](https://www.php.net/manual/en/function.serialize.php#107717) **14 years ago** ``` Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries. If you want your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query and modify data in the database without using a third party intermediary tool to manipulate data to be inserted. I've encountered this too many times in my career, it makes for difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized. That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare. ``` 6 [***mark at bvits dot co dot uk***](https://www.php.net/manual/en/function.serialize.php#128200) [¶](https://www.php.net/manual/en/function.serialize.php#128200) **3 years ago** ``` There is a type not mentioned in the user notes so far, 'E'. This is the newer Enum class that can be utilised: login_security|E:25:"Permission:manageClient" ``` 22 [***MC\_Gurk at gmx dot net***](https://www.php.net/manual/en/function.serialize.php#60317) [¶](https://www.php.net/manual/en/function.serialize.php#60317) **20 years ago** ``` If you are going to serialie an object which contains references to other objects you want to serialize some time later, these references will be lost when the object is unserialized. The references can only be kept if all of your objects are serialized at once. That means: $a = new ClassA(); $b = new ClassB($a); //$b containes a reference to $a; $s1=serialize($a); $s2=serialize($b); $a=unserialize($s1); $b=unserialize($s2); now b references to an object of ClassA which is not $a. $a is another object of Class A. use this: $buf[0]=$a; $buf[1]=$b; $s=serialize($buf); $buf=unserialize($s); $a=$buf[0]; $b=$buf[1]; all references are intact. ``` 24 [***nh at ngin dot de***](https://www.php.net/manual/en/function.serialize.php#111168) [¶](https://www.php.net/manual/en/function.serialize.php#111168) **13 years ago** ``` Serializing floating point numbers leads to weird precision offset errors: <?php echo round(96.670000000000002, 2); // 96.67 echo serialize(round(96.670000000000002, 2)); // d:96.670000000000002; echo serialize(96.67); // d:96.670000000000002; ?> Not only is this wrong, but it adds a lot of unnecessary bulk to serialized data. Probably better to use json_encode() instead (which apparently is faster than serialize(), anyway). ``` 12 [***Andrew B***](https://www.php.net/manual/en/function.serialize.php#109953) [¶](https://www.php.net/manual/en/function.serialize.php#109953) **13 years ago** ``` When you serialize an array the internal pointer will not be preserved. Apparently this is the expected behavior but was a bit of a gotcha moment for me. Copy and paste example below. <?php //Internal Pointer will be 2 once variables have been assigned. $array = array(); $array[] = 1; $array[] = 2; $array[] = 3; //Unset variables. Internal pointer will still be at 2. unset($array[0]); unset($array[1]); unset($array[2]); //Serialize $serializeArray = serialize($array); //Unserialize $array = unserialize($serializeArray); //Add a new element to the array //If the internal pointer was preserved, the new array key should be 3. //Instead the internal pointer has been reset, and the new array key is 0. $array[] = 4; //Expected Key - 3 //Actual Key - 0 echo "<pre>" , print_r($array, 1) , "</pre>"; ?> ``` 12 [***frost at easycast dot ru***](https://www.php.net/manual/en/function.serialize.php#113305) [¶](https://www.php.net/manual/en/function.serialize.php#113305) **12 years ago** ``` Closures cannot be serialized: <?php $func = function () {echo 'hello!';}; $func(); // prints "hello!" $result = serialize($func); // Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' ?> ```
Shard91 (laksa)
Root Hash2869005436169329891
Unparsed URLnet,php!www,/manual/en/function.serialize.php s443