开发者

the PHP variable handling function, serialize()

开发者 https://www.devze.com 2023-02-18 12:31 出处:网络
First off I\'ve read the PHP manual, I\'ve tested it out. Second I still don\'t understand what it r开发者_开发知识库eally does?

First off I've read the PHP manual, I've tested it out. Second I still don't understand what it r开发者_开发知识库eally does? Why would I want to serialize a variable?


You probably wouldn't want to serialise a variable as such, but it is useful to serialise objects and other complex data structures.

Instead of creating a database table with loads of columns, create a table with a primary key and a blob and serialise a class or array into it. That way you have an infinitly flexible system where if you need to add new data to the database table you don't have to add more columns.

This is one silly example, but persisting objects into a database is seriously useful if you think about it.


On reason might be to store the value in a text file, or a database. serialize() converts non-text values to text (such as binary integer or float values), so that they can easily be stored in this format, and easily converted back with unserialize()... if you actually look at a session file, that's the $_SESSION array stored as a serialized string that can easily be converted back to the $_SESSION array when you do session_start().


Serializing a single variable doesn't make much sense. it's far more useful to serialize an array or an object:

$array = array(
     0 => 'hello',
     1 => 'there',
     'how' => 'are',
     'you' => '?'
);

$txt = serialize($array);
echo($txt); //

produces

a:4:{i:0;s:5:"hello";i:1;s:5:"there";s:3:"how";s:3:"are";s:3:"you";s:1:"?";}

You can take this text string, store it in a database, send it via email, stuff it into a text file, etc... then later retrieve it and turn it back into a PHP array with a simple unserialize() call.


Serializing a variable means "converting it to a string". Basically, it is a way of converting a variable into a form that can be easily stored for future use.

Imagine this situation: you have an array containing lots of information. You need to store it in a text file to be ready for the next time you need it. By default, converting an array to a string has unexpected results. For instance, doing this:

file_put_contents('file.txt', array('foo', 'bar'));

puts the string foobar into your text file. You can't easily convert that back into the array. serialize converts the array to this:

file_put_contents('file.txt', serialize(array('foo', 'bar')));
// a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}

That isn't easy to read, but the unserialize function can convert it back into the array very easily.


The serialize function in PHP will convert a data structure into a storable string. Serialize data (e.g. a variable, array, or object) for storage in a database or other storage location. Once unserialized, you can reuse this data later after retrieving it. Here's an example:

$data = array("key"=>"value");
$serialized = serialize($data);

// Shows the serialized string representing the array:
// string(28) "a:1:{s:3:"key";s:5:"value";}"
var_dump($serialized);

$unserialized = unserialize($serialized);

// Shows the original array named $data:
// array(1) { ["key"]=>  string(5) "value" } 
var_dump($unserialized);


It writes the variable in a form capable of being transfered and stored. For example, you must serialize a variable before writing it in a file, in a database or simply for storing it in HTTP session

0

精彩评论

暂无评论...
验证码 换一张
取 消