The difference between php serialize() and unserialize()
This article mainly introduces some differences between php serialize() and unserialize(). Friends in need can refer to
The explanation of serialize() and unserialize() in the php manual is:
serialize — Generates a storable representation of a value
serialize
— Generates a storable representation of a value
unserialize — Creates a PHP value from a stored representation
unserialize
— Create a PHP value from a stored representation
Obviously, the interpretation of "a stored representation" is translated into a storable value It's still very confusing to understand what it means.
If the language cannot be expressed clearly, then we can use a specific PHP example to learn the use of these two functions
<?php //声明一个类 class dog { var $name; var $age; var $owner; function dog($in_name="unnamed",$in_age="0",$in_owner="unknown") { $this->name = $in_name; $this->age = $in_age; $this->owner = $in_owner; } function getage() { return ($this->age * 365); } function getowner() { return ($this->owner); } function getname() { return ($this->name); } } //实例化这个类 $ourfirstdog = new dog("Rover",12,"Lisa and Graham"); //用serialize函数将这个实例转化为一个序列化的字符串 $dogdisc = serialize($ourfirstdog); print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:"dog":3:{s:4:"name";s:5:"Rover";s:3:"age";i:12;s:5:"owner";s:15:"Lisa and Graham";} print '<BR>'; /* ----------------------------------------------------------------------- 在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 ----------------------------------------------------------------------- */ //我们在此注销这个类 unset($ourfirstdog); /* 还原操作 */ /* ----------------------------------------------------------------------- 在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 ----------------------------------------------------------------------- */ //我们在这里用 unserialize() 还原已经序列化的对象 $pet = unserialize($dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了 //获得年龄和名字属性 $old = $pet->getage(); $name = $pet->getname(); //这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态 print "Our first dog is called $name and is $old days old<br>"; print '<BR>'; ?>
We can also change the objects in the example to other types such as arrays, and the effect will be the same!
In fact, serialize() is to serialize the values of variables in PHP such as objects (objects), arrays (array), etc. into strings and store them. We can store the serialized strings in other places Such as database, Session, Cookie, etc., the serialization operation will not lose the type and structure of these values. In this way, the data of these variables can be passed between PHP pages and even different PHP programs.
And unserialize() is to convert the serialized string back to a PHP value.
Here is another quote from the PHP manual. After reading the above example, it should be easy to understand the meaning of the following words
Want to change the serialized string back to PHP For values, unserialize() can be used. 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 cleanup operations to be done before the object is serialized. Similarly, when using unserialize() to restore an object, the __wakeup() member function
will be called. unserialize() operates on a single serialized variable and converts it back to a PHP value. What is returned is the converted value, which can be integer, float, string, array or object. Returns FALSE if the passed string is not deserializable.
Related recommendations:
Detailed explanation of serialize() and unserialize() function examples in php
Detailed explanation of form formatting plug-in jquery.serializeJSON
Ajax serialize() form to serialize the file upload
The above is the detailed content of The difference between php serialize() and unserialize(). For more information, please follow other related articles on the PHP Chinese website!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
