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 to understand Php output buffering cache and program cache

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to understand Php output buffering cache and program cache". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Let's test the ob cache and program cache:

In order to make the test more effective before testing, we first turn off the ob cache in php.ini and set the obvious error level.

Output_buffering=off

Display_errors=on

Code 1:

The copy code is as follows:

Echo "php"

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

Will appear.

Php

Warning: Cannot modify header information-headers already sent by (output started at D:\ www\ apache\ htdocs\ test\ t2.php:2) in D:\ www\ apache\ htdocs\ test\ t2.php on line 3

Ok

Code 2:

The copy code is as follows:

Ob_start ()

Echo "php"

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

The result is absolutely right.

Cause analysis:

The code 1:Php has already sent a header message to the browser during echo 'php'

When it shows up again

Header ("content-type:text/html;charset='utf-8'")

When I saw another header message, I had already typed back the above header message, so I couldn't make a mistake.

Code 2: the ob cache is enabled. When echo 'php' puts the data to be called to the browser into the ob cache first, then encounters a header message, which is also put into the Ob cache. At the end of the page, press the http protocol to the program cache and return it to the browser.

To deepen our understanding, take a look at the following code

Code 3:

The copy code is as follows:

Ob_start ()

Echo "php"

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

Echo''

$ob=ob_get_contents ()

Echo $ob

Will output

Ob_get_contents () just fetches the contents of the ob cache without knowing them

Ob_get_contents () must be used before the ob cache is cleared

Code 4:

The copy code is as follows:

Ob_start ()

Echo "php"

Ob_clean (); / / clear the contents of the cache without closing the cache, you can also use (add something to it)

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

Echo''

$ob=ob_get_contents ()

Echo $ob

Results:

Code 5:

The copy code is as follows:

Ob_start ()

Echo "php"

Ob_end_clean (); / / clear the cache content and close the cache area. Ob_get_contents cannot get the content.

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

Echo''

$ob=ob_get_contents ()

Echo $ob

Results:

Code 6:

The copy code is as follows:

Ob_start ()

Echo "php"

Ob_end_flush (); / / send the cache to the program cache and close the ob cache

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

Echo''

$ob=ob_get_contents ()

Echo $ob

Code 7: compare Code 6 with ob_flush ()

The copy code is as follows:

Ob_start ()

Echo "php"

Ob_flush (); / / send the Ob cache to the program cache without closing the ob cache

Header ("content-type:text/html;charset='utf-8'")

Echo 'ok'

Echo''

$ob=ob_get_contents ()

Echo $ob

Results:

Ob_clean ()

Clear ob cache contents without closing

Ob_get_flush ()

Flush the cache to the program cache and turn off the ob cache

Code 8:

The copy code is as follows:

Ob_start ()

Echo 'abc'

Header ("content-type:text/html;charset='utf-8'")

Echo 'hello'

Ob_flush ()

Echo 'aa'

Echo ob_get_contents ()

/ / abchelloaaaa

2.ob_flush (), flush () and program cache

Code 9:

The copy code is as follows:

Ob_start ()

Echo 'a'

Flush (); / / flush the Ob cache to the program cache and then to the browser output, without affecting the ob cache

Echo ob_get_contents ()

/ / aa

Code 10:

The copy code is as follows:

Ob_start ()

Echo 'a'

Ob_flush (); / / flush the Ob cache to the program cache, and there is no cache content in the ob

Echo "ob_con" .ob _ get_contents ()

/ / An is output normally, and there is no content in Ob

Program cache:

Code 11:

The copy code is as follows:

Echo str_repeat (", 1024); / / some versions of Microsoft Internet Explorer start to display the page only after the 256bytes received, so you must send some extra spaces for these browsers to display the page content.

For ($iSuppli

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: 253

*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