Serialization variables and serialized objects in PHP

Source: Internet
Author: User
Variable | Object serialization is probably the transformation of some variables into a string of bytes of the form, so it is easier to transfer, storage. Of course, the switch is not what the transport storage, the key is to become a string form can be converted back, but also to maintain the original data structure.

In PHP, there are many serialization functions: Serialize (), which converts any variable value (in addition to the resource variable) into the form of a string, you can save the string to a file, or register as a session, and even use curl to simulate get/post to transfer variables, To achieve the effect of RPC.

If you want to convert a serialized variable to PHP's original variable value, you can use the Unserialize () function.

   One, the variable serialization

We give a simple example to illustrate serialization and its storage format.

Integral type:

$var = 23;
Echo Serialize ($var);

Output:

i:23;

Floating-point type:

$var = 1.23;

Echo Serialize ($var);

Output:

d:1.229999999999999982236431605997495353221893310546875;

String:

$var = "This is a string";
Echo Serialize ($var);
$var = "I am a variable";
Echo Serialize ($var);

Output:

S:16: "This is a string";
S:8: "I am a variable";

Boolean type:

$var = true;
Echo Serialize ($var);
$var = false;
Echo Serialize ($var);

Output:

B:1;
b:0;

After these basic types are serialized, it is clear that the storage format after serialization is:

Variable type: [variable length:] variable value;

Is that the first character represents the variable type, the second: represents the partition, the variable length is optional, is in the string type has, the other type does not, the last one is the variable value, each serialized value to ";" As the end.

Like our integer number 23 after serialization is: i:23, then it has no length, only the type and variable values, I is the integer, through the colon split, followed by the integer value of 23, including floating-point type (Double-byte type) is the same. Boolean, the type is B (Boolean), if true, then the serialized value is 1, and if it is false then the value is 0. Word

String value words will be saved in the middle of a value, save the length of the string, such as the string "This is a string", then the serialized value of the generated is s:16: "This is a string"; S is a string, representing the type, the middle 16 is the length of the string, if it is in Chinese, then each Chinese is two characters saved, such as the string "I am a variable", the generated serialization value is: s:8: "I am a variable"; is the length of 8 characters.

Now let's focus on the serialization of array variables.

Array variable:

$var = Array ("abc", "Def", "XYZ", "123");
Echo Serialize ($var);
Output:

A:4:{i:0;s:3: "abc"; I:1;S:3: "Def"; I:2;s:3: "XYZ"; I:3;s:3: "123";}
is to put the string value of my array $var serialized, our $var array consists of 4 string elements, namely "ABC", "Def", "XYZ", "123", and we analyze the serialized data, for the sake of simplicity, we have serialized data columns in the same style:

A:4:
{
I:0;s:3: "ABC";
I:1;s:3: "Def";
I:2;s:3: "XYZ";
I:3;s:3: "123";
}
This arrangement is more clear, look at the beginning of the string: a:4:{...} first character a saves the variable type as array (array) type, and the second 4 holds the number of array elements, a total of 4, and then the contents of the array element between {}. such as the first array element: I:0;s:3: "abc"; I represents the index value type of the current array element is an integral type, and the value is 0, the type of the element value is s (string), the number is 3, the value is "ABC", the semicolon ends, and the following array elements are followed by analogy.

Let's take a look at what it would be to use a string as an element index:

$var = Array ("INDEX1" => "abc", "Index2" => "def", "Index3" => "xyz", "index4" => "123");
Echo Serialize ($var);
Output:

A:4:{s:6: "Index1"; s:3: "abc"; S:6: "Index2"; s:3: "Def"; s:6: "Index3"; s:3: "XYZ"; s:6: "Index4"; S:3: "123";
After becoming an array style:

A:4:
{
S:6: "Index1"; s:3: "ABC";
S:6: "Index2"; s:3: "Def";
S:6: "Index3"; s:3: "XYZ";
S:6: "Index4"; S:3: "123";
}


In fact, not much different from the above, but the beginning of the index into the form of saving strings, such as the first element: s:6: "Index1"; s:3: "ABC"; The first is indexed: s:6: "Index1"; S is the type, 6 is the length of the index string, and "Index1" is the value of the index. Behind the s:3: "ABC"; is the element value, this good understanding, will not speak.

From the above, we have a general understanding of the basic data types of serialization, in fact, we can construct their own serialization functions, or from this perspective to expand, develop their own serialization program, easy to exchange our variables.

Of course, we can also use this function, the array or any other variables serialized into a string, and then through the curl function to simulate the get/post function, to achieve the useless user to perform the action from the remote server to obtain data from the function.

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.