Table of Contents
Output Control of PHP caching mechanism, outputcontrol
What is the caching mechanism of php?
How to use caching in php and which caching mechanism is best;
Home Backend Development PHP Tutorial Output Control of PHP caching mechanism, outputcontrol_PHP tutorial

Output Control of PHP caching mechanism, outputcontrol_PHP tutorial

Jul 13, 2016 am 10:23 AM
control output php mechanism cache

Output Control of PHP caching mechanism, outputcontrol

-------------------------- -------------------------------------------------- ----------------------------

In the configuration of version php5.2, output_buffering is turned off by default. Running the following three lines of code will result in a warning: Warning: Cannot modify header information - headers already sent

<span>echo</span> 'hello1'<span>;
</span><span>header</span>('content-type:text/html;charset=utf-8'<span>);
</span><span>echo</span> 'hello2';
Copy after login

Two ways to enable OB cache=>

1. Enable output_buffering = 4096 in php.ini

//# If this directive is enabled, then each PHP script is equivalent to calling the ob_start() function at the beginning. PHP5.5 has output_buffering = 4096 enabled by default

2. Directly in the program ob_start();

//# Turn on output buffering. When output buffering is activated, the script will not output content (except http headers), instead the content to be output is stored in an internal buffer.

//# The contents of the internal buffer can be copied to a string variable using the ob_get_contents() function. To output the content stored in the internal buffer, you can use the ob_end_flush() function. In addition, using the ob_end_clean() function will silently discard the contents of the buffer.

<span>/**<br /> * output_buffering = off 情况下测试<br /> * @chenwei <www.chenwei.ws><br /> */<br />ob_start</span>();    <span>//</span><span>开启ob缓存</span>
<span>echo</span> 'hello1'; <span>//</span><span>存入ob缓存</span>
<span>header</span>('content-type:text/html;charset=utf-8');<span>//</span><span>存入程序缓存<br /><br />//ob_end_clean(); //清空ob缓存,并关闭ob缓存<br /><br /></span>
Copy after login
<span>echo</span> 'hello2'; <span>//</span><span>存入ob缓存</span>
<span>$str</span> = <span>ob_get_contents</span>(); <span>//返回</span><span>ob缓存的数据(不清除缓冲内容)</span>
<span>file_put_contents</span>('ob.txt', <span>$str</span>); <span>//</span><span>把$str保存到文件</span>
<br />//ob_clean();  //清空ob缓存<br />
<span>echo</span> 'hello3'; <span>//</span><span>存入ob缓存</span>
<span>echo</span> 'hello4'; <span>//</span><span>存入ob缓存<br /><br />/* 此脚本将生成ob.txt文件,存入hello1hello2,浏览器输出hello1hello2hello3hello4 */<br />/* 若ob_clean()注释打开,那么生成的ob.txt文件中将没有内容,浏览器输出hello3hello4 */<br />/* 若ob_end_clean()注释打开,那么ob.txt中依然没有内容,因为关闭了ob缓存,浏览器输出hello2hello3hello4 */<br /></span>
Copy after login

ob_flush() and ob_end_flush() Example:

<span>ob_start</span><span>();
</span><span>echo</span> 'abc'<span>;    //存入ob缓存
</span><span>header</span>('content-type:text/html;charset=utf-8'<span>); //存入程序缓存
</span><span>echo</span> 'hello'<span>;   //存入ob缓存
</span><span>ob_flush</span><span>();    //将ob缓存中的内容输出到程序缓存,清空ob缓存,不关闭ob缓存<br />//ob_end_flush()  //将ob缓存中的内容输出到程序缓存,清空ob缓存,关闭ob缓存
</span><span>echo</span> 'aa'<span>;     //存入ob缓存
</span><span>echo</span> <span>ob_get_contents</span>();<br /><br />/* 最后输出abchelloaaaa */<br />/* 注释ob_flush,打开ob_end_flush,最后输出abchelloaa */
Copy after login

----------------------------------------@黑eyedpoet----------------------------------

Note: When output_buffering = 4096 is turned on
ob_end_clean() only closes the ob cache once (that is, when ob_start is turned on), and the system does not close it
Same as ob_end_flush().

Operating principle/principle of OB cache=>

 1. The ob cache is turned on, and the echo data is first put into the ob cache

2. If it is header information, put it directly in the program cache

 3. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser at once

 

Finally there is a flush(); to force refresh the PHP program cache to the browser cache.

//# Feature: Some versions of Microsoft Internet Explorer will only start displaying the page after receiving 256 bytes, so some extra spaces must be sent to allow these browsers to display the page content.

<span>echo</span> <span>str_repeat</span>('', 1024);<span>//</span><span>重复输出多个字符(解决浏览器缓存256字节之后再输出的情况)</span>
<span>for</span>(<span>$i</span>=0; <span>$i</span> < 5; <span>$i</span>++<span>)
{
    </span><span>echo</span> <span>$i</span><span>;
    </span><span>flush</span>();        <span>//</span><span>强制刷新程序缓存到浏览器缓存</span>
    <span>sleep</span>(1);      <span>//</span><span>休眠1秒钟,http连接未断开,每隔1秒输出$i</span>
}
Copy after login

-------------------------------------------------- -------------------------------------------------- --------

What is the caching mechanism of php?

Mainly include:
① Universal caching technology ② Page caching ③ Time-triggered cache ④ Content-triggered cache ⑤ Static cache (generate html files)
⑥ Memory cache ⑦ PHP buffer ⑧ MYSQL cache ⑨ Based on reverse proxy Web caching, DNS polling
But the commonly used ones are ①②④. Others are used when the website has a large amount of data and has many interactions.
is used to reduce the pressure on the server. Reference: blog.163.com/.. .44905/

How to use caching in php and which caching mechanism is best;

This depends on your actual situation. There are file cache, database cache, and memcache cache. . . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/833845.htmlTechArticleOutput Control of PHP caching mechanism, outputcontrol ---------------- -------------------------------------------------- --------------------------------------- In the configuration of php5.2 version,...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles