Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of PHP variable Serialization Storage format

Shulou Source: shulou.com Published: 2022-06-02 19:34:00 09月29日 Update

This article shares a sample analysis of PHP variable serialization storage formats. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

PHP is still more commonly used, so I studied PHP variable serialization, here to share with you, I hope useful to you. Serialization is roughly converting some variables into a byte stream of strings, which is easier to transmit and store. Of course, there is nothing to do with transmission and storage. The key is that it can be converted back into a string form and can maintain the original data structure.

Serialization is roughly converting some variables into a byte stream of strings, which is easier to transmit and store. Of course, there is nothing to do with transmission and storage. The key is that it can be converted back into a string form and can maintain the original data structure. In PHP there is a multi-serialization function: serialize(), which converts any variable value (except resource variables) into string form, saves the string to a file, registers it as a Session, or even uses curl to simulate GET/POST to transfer variables to achieve RPC effect. If you want to convert serialized variables to PHP raw variable values, you can use the unserialize() function.

PHP variable serialization

Let's take a simple example of PHP variable serialization and its storage format.

Integer:

$var = 23; echo serialize($var);

Output: i:23;

Floating point type:

$var = 1.23; echo serialize($var);

Output: d: 1.22999999999999998223643160599749535321893310546875;

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:

$var = true; echo serialize($var); $var = false; echo serialize($var);

Output: b:1;b:0;

The above basic types after serialization is very clear, the storage format after serialization is: variable type:[variable length:] variable value; that is, the *** bit character represents the variable type, the second: represents the partition, the variable length is optional, that is, in the string type, other types do not, *** one is the variable value, each serialized value ends with ";".

Thank you for reading! About "PHP variable serialization storage format example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Tags: Variables stores characters strings that is forms types transmissions formats values outputs functions examples analysis representatives keys content data more articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia MariaDB Apple Shulou Tech Info