PHP is still relatively commonly used, so I studied PHP variable serialization and shared it with you here. I hope it will be useful to everyone. Serialization is probably about converting some variables into a byte stream of strings, which makes it 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 string form and the original data structure can be maintained.
PHP variable serialization
We give a simple example to illustrate PHP variable serialization and its storage format.
Integer type:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">23</font></span><span>; </span></span></li> <li class=""><span>echo serialize($var); </span></li> </ol>
Output: i:23;
Floating point type:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">1</font></span><span>.23; </span></span></li> <li class=""><span>echo serialize($var); </span></li> </ol>
Output: d:1.229999999999999982236431605997495353221893310546875; is a string";s: 8: "I am a variable";
Boolean:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"This is a string"</font></span><span>; </span></span></li> <li class=""><span>echo serialize($var); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"我是变量"</font></span><span>; </span> </li> <li class=""><span>echo serialize($var); </span></li> </ol>
Output: b:1;b:0;
the above The situation after serialization of basic types is very clear. The storage format after serialization is: variable type: [variable length:] variable value; that is, the first character represents the variable type, the second: represents division, and the variable length is Optional, it is available in string types but not in other types. The last one is the variable value. Each serialized value ends with ";".
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">true</font></span><span>; </span></span></li> <li class=""><span>echo serialize($var); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">false</font></span><span>; </span> </li> <li class=""><span>echo serialize($var); </span></li> </ol>