Usage of php unserialize and serialize

巴扎黑
Release: 2016-11-24 13:58:18
Original
1536 people have browsed it

PHP provides two functions, one is serialize and the other is unserialize. So what are the uses of these two functions?

Serialization can serialize any data type in php except resource into a string. Here, we take an object as an example and define a class at will

class vb{

private $a;

private $ b;

function __construct($a='peng',$b='luo'){

$this->a=$a;

$this->b=$b;

}

We will $obj serialization

$c= serialize($obj); After printing out $c, we get O:2:"vb":2{s:5:"vba";s:4:"peng";s: 5:"vbb";s:3:"luo";}

We will deserialize this string again,

$c=unserialize($c); and print it out

print_r($c);


vb Object ( [a:private] => peng [b:private] => luo )

At this point we get an object, we can call the properties and methods in the object

echo $c-> ;speak();

Actually, these two functions are not difficult, but many people don’t know when to use them and what their uses are.

The biggest use of this function is that you want to save complex data types to When it is in a file or database, its effect can be revealed

Related labels:
php
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