详解PHP中的序列化、反序列化操作
数据(变量)序列化(持久化)
将一个变量的数据"转换为"字符串,但并不是类型转换,目的是将该字符串存储在本地。相反的行为成为反序列化。
流程:
//序列化 $str = serialize($r1); //保存到本地 file_put_contents("文本文件路径",$str); //从本地取出 $str2 = file_get_contents("文本文件路径"); //反序列化为之前的对象 $v1 = unserialize($str2);
具体实例:
1.在xxx1.php中进行序列化操作
<?php $v1 = 1; $v2 = 'abc'; $v3 = array('a'=>1,'bb'=>2.2,'awd',true); $str1 = serialize($v1); $str2 = serialize($v2); $str3 = serialize($v3); //写入文本文件 file_put_contents('./a1.txt', $str1); file_put_contents('./a2.txt', $str2); file_put_contents('./a3.txt', $str3); ?>
2.在xxx2.php中进行反序列化操作
<?php $s1 = file_get_contents('./a1.txt'); $s2 = file_get_contents('./a2.txt'); $s3 = file_get_contents('./a3.txt'); $var1 = unserialize($s1); $var2 = unserialize($s2); $var3 = unserialize($s3); echo "<br/>var_dump($var1,$var2,$var3)"; ?>
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23