Principles and applications of output buffering in PHP

藏色散人
Release: 2023-04-07 17:36:01
forward
1990 people have browsed it

php caching process

In the process of requesting a PHP, there are actually three caches:

1. Program cache

2 .ob cache

3. Browser cache.

Two methods to open ob

1. Configure in php.ini; output_buffering = 4096 Just remove the ; sign here

2 Use ob_start() in the php page;

If opened through php.ini, it will apply to all php pages. Opening with ob_start() only affects the page

Knowledge points of ob cache

In the service, if we enable ob cache, the echo data is first put Enter ob

When the PHP page is executed to the end, the ob cached data (if any) will be forcibly refreshed to the program cache, and then the data will be encapsulated into an http response package through apache and returned to Browser

If there is no ob, all data will be directly put into the program cache. The header information is always put into the program cache regardless of whether you enable ob.

ob related functions

ob_start($callback)

//在当前页面中开启ob,注意callback
ob_start($callback);
Copy after login

ob_get_contents()

//获取当前ob缓存中的内容
ob_get_contents()
Copy after login

ob_get_clean()

//获取当前ob缓存中的内容,并且清空当前的ob缓存
ob_get_clean()
Copy after login

ob_flush()

//将ob缓存中的内容,刷到程序缓存中,但并没有关闭ob缓存
ob_flush()
Copy after login

ob_end_flush()

//关闭ob缓存,并将数据刷回到程序缓存中
ob_end_flush()
Copy after login

ob_clean()

//将ob缓存中的内容清空
ob_clean()
Copy after login

ob_end_clean()

//将ob缓存中的数据清空,并且关闭ob缓存
ob_end_clean()
Copy after login

Note ob_start($callback) Callback

<?php
ob_start("callback_func");
function callback_func($str){
    return "callback".$str;
}
echo "123";//输出:callback123
Copy after login

Application scenarios

Error reporting before header() is sent

Error code

<?php
echo "before_header";
header("Content-type:text/html;charset=utf-8");
echo "after_header";
Copy after login

Output:

Warning: Cannot modify header information - headers already sent by (output started at /Users/shuchao/Desktop/test.php:2) in /Users/shuchao/Desktop/test.php on line 3
Copy after login

Solution

Enable ob before sending the header, then all the echo content will go to ob, thus solving the error.

<?php
ob_start();
echo "before_header\n";
header("Content-type:text/html;charset=utf-8");
echo "after_header\n";
Copy after login

Output

before_header
after_header
Copy after login

The above is the detailed content of Principles and applications of output buffering in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
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