Home > Article > Backend Development > Detailed introduction to serialize in PHP
serialize
serialize() Returns a string. This string contains a byte stream representing value. Can be stored anywhere.
This facilitates storing or passing PHP values without losing their type and structure.
If you want to change the serialized string back to PHP value, you can use unserialize().
serialize() can handle any type except resource. You can even serialize() arrays that contain references to themselves.
References in the array/object you are serializing() will also be stored.
When serializing an object, PHP will attempt to call the object's member function __sleep() before the sequence action.
This allows any clearing operations to be done before the object is serialized. Similarly, when an object is restored using unserialize(), the __wakeup() member function will be called.
Note:
In PHP 3, object properties will be serialized, but methods will be lost. PHP 4 breaks this limitation and can store properties and methods at the same time. See the Serialized Objects section in Classes and Objects for more information.
Example
$arr = array ( 'liqingbo'=> '李清波' ,'zhangxueyou'=> '张学友' ,'guofucheng'=> '郭富城' ); $seri = serialize($arr);
Output:
a:3:{s:8:"lciqingbo";s:9:"李清波";s:11:"zhangxueyou";s:9:"张学友";s:10:"guofucheng";s:9:"郭富城";}
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of Detailed introduction to serialize in PHP. For more information, please follow other related articles on the PHP Chinese website!