14-6-2

7

、序列化

 

你有没有遇到过需要在数据库或文本文件存储一个复杂变量的情况?你可能没

能想出一个格式化字符串并转换成数组或对象的好方法,

PHP 

已经为你准备好此

功能。有两种序列化变量的流行方法。下面是一个例子,使用

 serialize() 

 

unserialize() 

函数:

 

// a complex array 

$myvar = array( 

 

'hello', 

 

42, 

 

array(1,'two'), 

 

'apple' 

); 

 

// convert to a string 

$string = serialize($myvar); 

 

echo $string; 

/* prints 

a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:"two";}i:3;s:5

:"apple";} 

*/ 

 

// you can reproduce the original variable 

$newvar = unserialize($string); 

 

print_r($newvar); 

/* prints 

Array 

    [0] => hello 

    [1] => 42 

    [2] => Array 

        ( 

            [0] => 1 

            [1] => two 

        ) 

 

    [3] => apple 

*/ 

这是原生的

 PHP 

序列化方法。

然而,

由于

 JSON 

近年来大受欢迎,

PHP5.2 

中已

经加入了对

 JSON 

格式的支持。现在你可以使用

 json_encode() 

 

json_decode() 

函数:

 

// a complex array 

$myvar = array( 

 

'hello', 

 

42, 

 

array(1,'two'), 

 

'apple' 

); 

 

// convert to a string 

$string = json_encode($myvar); 

 

echo $string; 

/* prints 

["hello",42,[1,"two"],"apple"] 

*/ 

 

// you can reproduce the original variable 

$newvar = json_decode($string); 

 

print_r($newvar); 

/* prints 

Array 

    [0] => hello 

    [1] => 42 

    [2] => Array 

        ( 

            [0] => 1 

            [1] => two 

        ) 

 

    [3] => apple 

*/ 

这将更为行之有效,

尤其与

 JavaScript 

等许多其他语言兼容。

然而对于复杂的

对象,某些信息可能会丢失。

 

8

、压缩字符串

 

在谈到压缩时,

我们通常想到文件压缩,

 ZIP 

压缩等。

 PHP 

中字符串压缩

也是可能的,

但不涉及任何压缩文件。

在下面的例子中,

我们要利用

 gzcompress() 

 gzuncompress() 

函数:

 

$string = 

"Lorem ipsum dolor sit amet, consectetur 

adipiscing elit. Nunc ut elit id mi ultricies 

adipiscing. Nulla facilisi. Praesent pulvinar, 

sapien vel feugiat vestibulum, nulla dui pretium orci, 

non ultricies elit lacus quis ante. Lorem ipsum dolor 

sit amet, consectetur adipiscing elit. Aliquam 

pretium ullamcorper urna quis iaculis. Etiam ac massa 

sed turpis tempor luctus. Curabitur sed nibh eu elit 

mollis congue. Praesent ipsum diam, consectetur vitae 

ornare a, aliquam a nunc. In id magna pellentesque 

tellus posuere adipiscing. Sed non mi metus, at lacinia 

augue. Sed magna nisi, ornare in mollis in, mollis 

sed nunc. Etiam at justo in leo congue mollis. 

Nullam in neque eget metus hendrerit scelerisque 

eu non enim. Ut malesuada lacus eu nulla bibendum 

id euismod urna sodales. "; 

 

$compressed = gzcompress($string); 

 

echo "Original size: ". strlen($string)." "; 

/* prints 

Original size: 800 

*/ 

 

echo "Compressed size: ". strlen($compressed)." "; 

/* prints 

Compressed size: 418 

*/ 

 

// getting it back 

$original = gzuncompress($compressed); 

这种操作的压缩率能达到

 50% 

左右。另外的函数

 gzencode() 

 gzdecode() 

能达到类似结果,通过使用不同的压缩算法。

 

9

、注册停止功能

 

有一个函数叫做

 register_shutdown_function()

,可以让你在某段脚本完成运

行之前,

执行一些指定代码。

假设你需要在脚本执行结束前捕获一些基准统计信

息,例如运行的时间长度:

 

// capture the start time 

$start_time = microtime(true); 

 

// do some stuff 

// ... 

 

// display how long the script took 

echo "execution took: ". 

 

 

(microtime(true) - $start_time). 

 

 

" seconds."; 

这似乎微不足道,

你只需要在脚本运行的最后添加相关代码。

但是如果你调用过

 

exit() 

函数,该代码将无法运行。此外,如果有一个致命的错误,或者脚本被

用户意外终止,

它可能无法再次运行。

当你使用

 register_shutdown_function() 

函数,代码将继续执行,不论脚本是否停止运行:

 

$start_time = microtime(true); 

 

register_shutdown_function('my_shutdown'); 

 

// do some stuff 

// ... 

 

function my_shutdown() { 

 

global $start_time; 

 

 

echo "execution took: ". 

 

 

 

(microtime(true) - $start_time). 

 

 

 

" seconds."; 

}

原文地址:https://www.cnblogs.com/huzhen/p/3764832.html