Home>Article>Backend Development> Principles and applications of output buffering in PHP
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);
ob_get_contents()
//获取当前ob缓存中的内容 ob_get_contents()
ob_get_clean()
//获取当前ob缓存中的内容,并且清空当前的ob缓存 ob_get_clean()
ob_flush()
//将ob缓存中的内容,刷到程序缓存中,但并没有关闭ob缓存 ob_flush()
ob_end_flush()
//关闭ob缓存,并将数据刷回到程序缓存中 ob_end_flush()
ob_clean()
//将ob缓存中的内容清空 ob_clean()
ob_end_clean()
//将ob缓存中的数据清空,并且关闭ob缓存 ob_end_clean()
Note ob_start($callback) Callback
Application scenarios
Error reporting before header() is sent
Error code
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 3Solution
Enable ob before sending the header, then all the echo content will go to ob, thus solving the error.
Output
before_header after_headerThe 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!