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['keys'] = 'www'; $array['values']='11111'; $a = serialize($array); echo $a; unset($array); $a = unserialize($a); print_r($a); ?>
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!