Example explanation of serialize() and unserialize() functions

零下一度
Release: 2023-03-13 07:42:01
Original
1186 people have browsed it

Today when I was looking at the class.quickskin.PHP code, I saw a function serialize that I had never seen before. Both Master and Xiaobai asked about it, but they couldn’t figure it out. As a result, I thought about testing it locally, and then The pig's head hit once. . . .
In fact, this kind of problem is very simple and can be solved by ourselves. Many times we are too lazy. . . .
It is often difficult to explain a program in language, especially for a novice like me. Many times it is easier to just look at the code.
The explanation in the PHP5 manual is as follows. It feels a bit confusing at first glance, but after reading it again after testing, it will become clearer.
Simply put, serialize($param) is to serialize the value of $param. It seems that some fields in the UCHOME database are stored in such data (a:1:{s:7:"apppath";s :0:"";}), and unserialize($param) is like the reverse operation of serialize, converting a serialized value into the corresponding string. This conversion is feasible for numeric variables and string variables, but for arrays and objects The examples output "Array" and "Catchable fatal error: Object of class Object could not be converted to string in" error messages respectively.

serialize() can handle all types except resource types, and can also serialize objects

<?php 
$array = array(); 
$array[&#39;keys&#39;] = &#39;www&#39;; 
$array[&#39;values&#39;]=&#39;11111&#39;; 
$a = serialize($array); 
echo $a; 
unset($array); 
$a = unserialize($a); 
print_r($a); 
?>
Copy after login

Output

a:2:{s:4:"keys" ;s:3:"www";s:6:"values";s:5:"11111";}

Array ( [keys] => www [values] => 11111 )

The same applies to classes

The above is the detailed content of Example explanation of serialize() and unserialize() functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!