Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PHP outputs caching

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how PHP output cache, Xiaobian feel quite practical, so share to everyone as a reference, I hope you can have some harvest after reading this article.

First of all, let's clarify the output order of PHP.

1. Open php output buffer: echo,print -> php output_buffering-> server buffering -> browser buffering -> browser display

2. php output cache not open: echo,print -> server buffering -> browser buffering -> browser display

Also clarify the browser output cache:IE is 256Bytes, Chrome and FireFox is 1000Bytes, only when the output data reaches this length or the script ends, the browser will output the data on the page.

Here are a few PHP settings and APIs used:

1.output_buffering configuration in php.ini

·Off: Indicates that PHP output caching is turned off

·On: Open infinite output cache

4096: Open output cache with size of 4096Byte

2. Implicit_flush configuration in php.ini

·On: means that after each output (such as echo,print), the flush() function is automatically called, and the output is directly output.

·Off: Contrary to On, flush() is not called after each output, and it needs to wait until the server buffering is full to output, but we can use flush() function instead of it, it doesn't matter if it is not turned on, but it is more flexible.

ob_flush () function: remove data from PHP buffering and put it into server buffering

4. flush () function: take out the data of Server buffering and put it into browser buffering

5.ob_start() function: I don't know much about this function now, because the output will not be controlled by ob_flush() after it is turned on, even if ob_flush() and flush() are used, the data will not be output immediately on the browser. Now we know that if output_buffering=Off, even if ob_start() is used, it is impossible to cache the output data, and if output_buffering=On, even if ob_start() is not used, the output data can also be cached by PHP, so ob_start is relatively useless, temporarily ignore it.

Then let's look at the code (set output_buffering= 4096, implicit_flush =Off)

The copy code is as follows:

Here the code output result is a line of output, the specific principle we refer to ob_flush() and flush() function efficacy

If either of these functions is missing, in my setup, you wait until 0, 1, 2, 3, 4 are cached and then you output them together.

Finally, a quote from Laruence's blog, I hope to help you understand

ob_flush/flush is described in the manual, is to refresh the output buffer, and also need to be used in conjunction, so many people will be confused…

In fact, the two of them operate differently, and in some cases flush doesn't do anything at all.

ob_* series of functions, PHP itself is the operation of the output buffer.

So ob_flush is a buffer that flushes PHP itself.

Flush, strictly speaking, is only useful if PHP is installed as a Module(handler or filter) for apache. It is a buffer that flushes Web Server (which can be thought of as apache).

In Apache module's sapi, flush flushes the apache output buffer indirectly by calling apache's api: ap_flush member function pointer by calling sapi_module's flush member function pointer. Of course, the manual also says that there are other apache modules that may change the result of this action.

1. Some Apache modules, such as mod_gzip, may cache their own output,

2. This will cause the flush() function to produce results that are not immediately sent to the client browser.

3.

4. Even browsers cache received content before displaying it. such as Netscape

5. Browsers cache content until they accept the start of a newline or html tag and

6. The entire table is not displayed until the tag is accepted.

7.

8. Some versions of Microsoft Internet Explorer are only accepted when up to 256

9. The page doesn't start displaying until after the byte, so you have to send some extra spaces to make this

10. Some browsers display page content.

So, the correct order to use them is. ob_flush first, then flush,

Of course, in other sapi, do not call flush can also, but in order to ensure the portability of your code, it is recommended to use supporting.

About "PHP how to output cache" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report