Home > Backend Development > PHP Tutorial > Detailed introduction to serialize in PHP

Detailed introduction to serialize in PHP

coldplay.xixi
Release: 2023-04-08 20:22:02
forward
3204 people have browsed it

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);
Copy after login

Output:

a:3:{s:8:"lciqingbo";s:9:"李清波";s:11:"zhangxueyou";s:9:"张学友";s:10:"guofucheng";s:9:"郭富城";}
Copy after login

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!

Related labels:
source:liqingbo.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template