In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "detailed explanation of PHP caching mechanism". In daily operation, I believe many people have doubts about the detailed explanation of PHP caching mechanism. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "detailed explanation of PHP caching mechanism"! Next, please follow the editor to study!
In the php5.2 version of the configuration, the default output_buffering is turned off, so running the following three lines of code will give you a warning:
Warning: Cannot modify header information-headers already sent
Echo 'hello1';header (' content-type:text/html;charset=utf-8'); echo 'hello2'
There are two ways to enable OB cache:
1. Enable output_buffering = 4096 in php.ini
If this directive is enabled, each PHP script is equivalent to calling the ob_start () function at the beginning. PHP5.5 has output_buffering = 4096 enabled by default.
two。 Use ob_start () directly in the program
Turn on output buffering. When output buffering is activated, the script will not output the content (except for the http header). Instead, the content that needs to be output is stored in the internal buffer.
The contents of the internal buffer can be copied into a string variable using the ob_get_contents () function. To output what is stored in the internal buffer, you can use the ob_end_flush () function. In addition, using the ob_end_clean () function silently discards the contents of the buffer.
/ * output_buffering = off test * / ob_start (); / / turn on ob cache echo 'hello1'; / / save ob cache header (' content-type:text/html;charset=utf-8'); / / save program cache / / ob_end_clean (); / / clear ob cache and close ob cache echo 'hello2'; / / save ob cache $str = ob_get_contents () / / return the data cached by ob (without clearing the buffer) file_put_contents ('ob.txt', $str); / / save $str to the file / / ob_clean (); / / clear the ob cache echo' hello3'; / / save to the ob cache echo 'hello4' / / save to ob cache / * this script will generate ob.txt file, save it to hello1hello2, browser output hello1hello2hello3hello4 * / / * if ob_clean () comment is opened, then the generated ob.txt file will have no content, browser output hello3hello4 * / / * if ob_end_clean () comment is opened, then ob.txt still has no content, because ob cache is turned off, browser output hello2hello3hello4 * /
Examples of ob_flush () and ob_end_flush ():
Ob_start (); echo 'abc';// is stored in ob cache header (' content-type:text/html;charset=utf-8'); / / stored in program cache echo 'hello'; / / stored in ob cache ob_flush () / / output the contents of the ob cache to the program cache, clear the ob cache, do not close the ob cache / / ob_end_flush () / / output the contents of the ob cache to the program cache, clear the ob cache, close the ob cache echo 'aa'; / / save the ob cache echo ob_get_contents (); / * finally output abchelloaaaa * / / * comment ob_flush, open ob_end_flush, and finally output abchelloaa * /
Note:
When output_buffering = 4096 is enabled, ob_end_clean () closes the ob cache only once (that is, ob_start is enabled), and the system is not closed.
Ob_end_flush () is the same.
How OB caching works / principles:
1. Ob cache is turned on, and echo data is first put into ob cache.
two。 If it is header information, put it directly in the program cache.
3. When the page is executed at the end, the data cached by ob is put into the program cache and returned to the browser one at a time
Finally, there is a flush (); forces the PHP program cache to be flushed to the browser cache.
Features: some versions of Microsoft Internet Explorer start to display the page only after receiving 256bytes, so you have to send some extra spaces for these browsers to display the page content.
Echo str_repeat (', 1024); / / repeat output of multiple characters (to solve the case that the browser caches 256bytes before output) for ($iS0; $I < 5; $iBytes +) {echo $I; flush (); / / forces the refresh program to cache to the browser cache sleep (1) / / dormant for 1 second, the http connection is not disconnected, and output $I} every 1 second. The study on "detailed understanding of PHP caching mechanism" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.