Home > Backend Development > PHP Tutorial > Knowledge Collection: Introduction to PHP variable serialization storage format_PHP tutorial

Knowledge Collection: Introduction to PHP variable serialization storage format_PHP tutorial

WBOY
Release: 2016-07-15 13:27:48
Original
829 people have browsed it

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.

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. There are multiple serialization functions in PHP: serialize(). This function converts any variable value (except resource variables) into the form of a string. The string can be saved to a file, or registered as a Session, or even Use curl to simulate GET/POST to transfer variables to achieve the effect of RPC. If you want to convert serialized variables into PHP original variable values, you can use the unserialize() function.

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

http://www.bkjia.com/PHPjc/446492.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/446492.htmlTechArticlePHP is still relatively commonly used, so I studied PHP variable serialization and share it with you here. Here's a look, hope it's useful to everyone. Serialization is probably about converting some variables into...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template