[转]PHP ob_start() and ob_start('ob_gzhandler')

来源:互联网 发布:魔兽数据库 编辑:程序博客网 时间:2024/05/21 19:33

question:

What is the difference between using ob_start() and ob_start('ob_gzhandler') ?
How does it affect the page speed ?

answer:

This doesn't affect page speed in the sense you'd might think.

the ob_gzhandler is a callback function which takes the contents from your output buffer and compresses the data before outputting it.

This reduces the size of the content being sent to the browser which might speed up the content transfer to the client. But it doesn't speed up your application/website.

PHP生成网页后传送给浏览器显示,生成的网页如果不经过处理,HTML将作原样输出。没经过压缩的网页容量有多大呢?我们来做个实验。我们用ZendFramework制作了一个有用户列表,并以表格输出其中的50行数据。
输出前未作压缩的页面大小,如下:


然后,我们在这个基础上在引导文件index.php加上
[php]ob_start('gz_handler');[/php]
输出后的页面大小,压缩率是相当可观的。如下:



根据压缩原理我们知道,当文档中存在大量相同代码时,压缩率相对较高。
我们在写html代码时,有大量的缩进、空格、代码相同的表格数据。所以HTML的压缩率一般都比较高。
经过压缩后的代码传到浏览器后都能被现代流行的浏览器正确解析,我们查看源代码时依旧能看到整齐的代码。
只是在输出到浏览器的网络传输上,原来20K的内容被压缩至2K传送,大大提高了网页的响应速度。
压缩PHP输出代码的好处显而易见。
0 0