search
Homephp教程php手册知识宝典:介绍PHP变量串行化存储格式

知识宝典:介绍PHP变量串行化存储格式

Jun 13, 2016 am 11:04 AM
phpintroducevariablestorageFormatCompareuseKnowledgeResearchstill

PHP还是比较常用的,于是我研究了一下PHP变量串行化,在这里拿出来和大家分享一下,希望对大家有用。串行化大概就是把一些变量转化成为字符串的字节流的形式,这样比较容易传输、存储。当然,关是传输存储没有什么,关键是变成串的形式以后还能够转化回来,而且能够保持原来数据的结构。

串行化大概就是把一些变量转化成为字符串的字节流的形式,这样比较容易传输、存储。当然,关是传输存储没有什么,关键是变成串的形式以后还能够转化回来,而且能够保持原来数据的结构。在PHP中有多串行化处理的函数:serialize(),该函数把任何变量值(除了资源变量)转化为字符串的形式,可以把字符串保存到文件里,或者注册为Session,乃至于使用curl来模拟GET/POST来传输变量,达到RPC的效果。如果要将串行化的变量转化成PHP原始的变量值,那么可以使用unserialize()函数。

PHP变量串行化

我们举简单的例子来说明PHP变量串行化,以及它的存储格式。

整型

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

输出:i:23;

浮点型

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

输出:d:1.229999999999999982236431605997495353221893310546875;

字符串

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

输出:s:16:"This is a string";s:8:"我是变量";

布尔型

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

输出:b:1;b:0;

上面这些基本类型串行化之后的情况很清楚,串行化之后的存储格式是:变量类型:[变量长度:]变量值;就是第一位字符代表变量类型,第二个:代表分割,变量长度是可选的,就是在字符串类型里有,其他类型没有,最后一个就是变量值,每个串行化的值以";"作为结束。


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software