2

First off I've read the PHP manual, I've tested it out. Second I still don't understand what it really does? Why would I want to serialize a variable?

2
  • I sort of get that it generates the type and structure, e.g s:12:"hello, world"; but why would I use it? and what instances would I use it in? Commented Mar 18, 2011 at 15:53
  • 4
    en.wikipedia.org/wiki/Serialization Commented Mar 18, 2011 at 15:54

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's not a great example, IMO. Storing data as a serialised string in a database means you can't use normal database queries on it. It's occasionally useful, but it's not the main utility of the function.
Thats why I said its one silly example, I find it useful to serialise a shopping cart object into a database so their shopping carts can easily be unserialised and made active, invoices generated or whatever.
5

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().

Comments

2

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.

3 Comments

What about encoding a serialised object and sending it to a remote web service instead of json or xml, or is that just crazy?
JSON and XML are far more universal in terms of support - I can't think of any web-aware language that doesn't support either directly or have a library to add the support. PHP's serialize is a bit more language-specific. Not to say you can't consume such a string in (say) Java or C#, but since you're going cross-language, might as well use something more suited like XML/JSON.
True, this would only be useful if you were creating your own distributed system rather than a web service.
1

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.

Comments

1

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);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.