把复杂的数据类型压缩到一个字符串中 serialize()把变量和它们的值编码成文本形式 unserialize()恢复原先变量 eg:stoogesarray(Moe,Larry,Curly);newserialize(stooges);printr(new);printr(unserialize(new)); 结果:a:3:{i:0;s:3:Mi:1;s:5:Li:2;s:5:C} Array(〔0〕Moe〔1〕Larry〔2〕Curly) 当把这些序列化的数据放在URL中在页面之间会传递时,需要对这些数据调用urlencode(),以确保在其中的URL元字符进行处理:shoppingarray(Poppyseedbagel2,PlainBagel1,Lox4); margicquotesgpc和magicquotesruntime配置项的设置会影响传递到unserialize()中的数据。 如果magicquotesgpc项是启用的,那么在URL、POST变量以及cookies中传递的数据在反序列化之前必须用stripslashes()进行处理:newcartunserialize(stripslashes(cart));如果magicquotesgpc开启newcartunserialize(cart); 如果magicquotesruntime是启用的,那么在向文件中写入序列化的数据之前必须用addslashes()进行处理,而在读取它们之前则必须用stripslashes()进行处理:fpfopen(tmpcart,w);fputs(fp,addslashes(serialize(a)));fclose(fp);如果magicquotesruntime开启newcatunserialize(stripslashes(filegetcontents(tmpcart)));如果magicquotesruntime关闭newcatunserialize(filegetcontents(tmpcart));在启用了magicquotesruntime的情况下,从数据库中读取序列化的数据也必须经过stripslashes()的处理,保存到数据库中的序列化数据必须要经过addslashes()的处理,以便能够适当地存储。mysqlquery(insertintocart(id,data)values(1,。addslashes(serialize(cart))。));rsmysqlquery(selectdatafromcartwhereid1);obmysqlfetchobject(rs);如果magicquotesruntime开启newcartunserialize(stripslashes(obdata));如果magicquotesruntime关闭newcartunserialize(obdata); 当对一个对象进行反序列化操作时,PHP会自动地调用其wakeUp()方法。这样就使得对象能够重新建立起序列化时未能保留的各种状态。例如:数据库连接等。