PHP输出缓冲及其应用
缓冲(buffer)是为了协调吞吐速度相差很大的设备之间数据传送而采用的技术,用来存放缓冲数据的区域叫缓冲区,在计算机科学领域,当数据从一个地方传送到另一个地方时,缓冲区被用来临时存储数据。与缓冲相似的一个技术是缓存(cache),它们都是为了解决数据存储和传输速度不同而带来的问题,不同的是,缓冲主要在写时使用,而缓存主要用来在读时使用。
如上图,是一个简易的缓冲区模拟图,左端入口的数据具有单个输入体积小,速度快,数量多,但右端输出数据具有体积大,速度慢的特点。如果没有缓冲区,很容易造成数据堵塞,有了缓冲区之后,当数据填满缓冲区,再统一输出,则可以大大减少系统负担。
PHP在执行的过程中,嵌入的HTML代码,’echo’,’print_r’等指令都是一次数据输出,正是因为有缓冲区的存在,系统可以在php执行完之后再一次把数据发送给浏览器,运行如下代码:
<?phpecho "这里是第一行数据";echo "这里是第二行数据,下面睡眠5秒";sleep(5);echo "这里是第三行数据,下面是HTML代码";?><h1 id="标题">标题</h1>
发现浏览器是同时显示所有内容,而不是先显示第一行和第二行数据,等待5秒后再显示后面的数据。不仅这样,PHP的缓冲区还提供给我们更加强大的功能,我们可以在数据发送之前对其作出捕获,更改等。PHP提供给我们”ob_”系列函数,例如如下代码,可以对某些字符进行替换:
<?phpob_start();echo "Hello world, this is http://www.hitoy.org/";$content = ob_get_contents();ob_end_clean();echo str_replace("http://","https://",$content);?>
上面中的ob_start,ob_get_contents,ob_end_clean分别用来开启用户缓冲区,获取缓存内容和关闭缓存区,PHP中所有的输出控制函数有:
flush — 刷新输出系统缓冲
ob_clean — 清空(擦掉)输出缓冲区
ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲
ob_end_flush — 冲刷出(送出)输出缓冲区内容并关闭缓冲
ob_flush — 冲刷出(送出)输出缓冲区中的内容
ob_get_clean — 得到当前缓冲区的内容并删除当前输出缓。
ob_get_contents — 返回输出缓冲区的内容
ob_get_flush — 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区。
ob_get_length — 返回输出缓冲区内容的长度
ob_get_level — 返回输出缓冲机制的嵌套级别
ob_get_status — 得到所有输出缓冲区的状态
ob_gzhandler — 在ob_start中使用的用来压缩输出缓冲区中内容的回调函数。ob_start callback function to gzip output buffer
ob_implicit_flush — 打开/关闭绝对刷送
ob_list_handlers — 列出所有使用中的输出处理程序。
ob_start — 打开输出控制缓冲
output_add_rewrite_var — 添加URL重写器的值(Add URL rewriter values)
output_reset_rewrite_vars — 重设URL重写器的值(Reset URL rewriter values)
php中,可以通过php.ini的output_buffering来设置缓存,On表示无穷大,Off表示关闭,数字则表示缓冲区的大小(以字节为单位),默认大小是4KB,如果设置成off,则示例一的代码是不是就可以分段在浏览器显示了呢?答案是否定的,有两点需要注意,第一点即使把PHP的缓存关闭,php输出在系统层面也有缓存(可以理解为Linux系统stdout的缓存),必须通过flush函数输出;第二点是一些有些浏览器对一次接收的文字长度有限制,如果太少,则不予显示。所以这样的代码可以分段显示:
<?phpecho "这里是第一行数据";echo str_repeat(" ",1024);echo "这里是第二行数据,下面睡眠5秒";flush();sleep(5);echo "这里是第三行数据,下面是HTML代码";?><h1 id="标题">标题</h1>
当然在实际生产环境,直接把output_buffering关闭的情况比较少见,我们可以通过ob_系列函数来进行操作,下面的例子是利用缓冲进行服务器推送(comet)的示例。如下代码可以给客户端进行信息推送:
<?phpob_start();$i=0;while($i<100){ echo $i.str_repeat(" ",2024); $i++; ob_flush(); flush(); sleep(5);}?>
参考资料
-
Output Control 函数
-
关于php_buffering的疑问

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
