| This article introduces the usage of php output cache. in php, echo or other output commands in the code are first written to php buffer during execution, data is output to the browser only after the script is executed or the output cache command is executed forcibly. I. What is php output cache?In php, the so-called output cache means that the echo or other output commands in the code are first written to the php buffer during execution. after the script is executed or the output cache command is forcibly executed, the data will be output to the browser (where php buffer is php. output_buffering set in ini. the default value is on, indicating unlimited size. you can change it to a number to limit the size ). Example: Echo 'hlmblog. com'; echo 'techno'; echo 'share ';These two echo values are inserted to the buffer in sequence. data is output to the browser only after the script execution is complete or the cache output is enforced. Output echo data in real time: Ob_end_flush (); // disable the php Cache, or execute ob_flush () before flush (). The following describes echo str_pad ("", 256 ); for ($ I = 5; $ I> 0; $ I --) {echo $ I.' '; Flush (); sleep (1 );}Note: 1: The difference between flush and ob_flush: At first glance, the two are very similar, and many manuals are unclear. In fact, the two are quite different. When php. when ini does not enable the php buffer cache, the content output by the php script will be in the waiting output status on the server and will not be saved to the output cache because the cache is not enabled, in this case, flush can be used to immediately output the content waiting for output to the client (browser or other output terminals ). When php. after ini enables the php buffer cache, the first step of the php script output content is stored in the output cache. at this time, when the output content is no data, it will be ineffective if flush is used, data is not retrieved. Therefore, you must first use ob_flush to retrieve the content in the output cache and change it to the waiting state. Then, use flush to send the content to the client. The execution sequence is ob_flush and then flush. Therefore, to achieve real-time output, either use ob_end_flush to disable the php output cache and then directly flush, or use ob_flush to flush. 2: The browser cannot output the modified code of real-time data. in chrome firefox ie and other browsers, the code is output at one time: Ob_end_flush (); // disable php Cache, or ob_flush (); echo str_pad ("", 256); for ($ I = 5; $ I> 0; $ I --) {echo $ I; flush (); sleep (1 );}You only need to add html tags for real-time output. The reason is: it is amazing that html tags are output immediately only when html tags are encountered. Fortunately, html tags are usually included in the output content and few plain texts are used. Solution: Add a carriage return or other html tags to solve the problem. II. example of controlling cache output 1. generating static pages loading speed is fast Echo str_pad ('', 1024); // buffer overflow ob_start (); // open the buffer $ content = ob_get_contents (); // get the page Output Content $ f = fopen ('. /index.html ', 'w'); fwrite ($ f, $ content); // write the txt file fclose ($ f); ob_end_clean (); // clear and disable the buffer2. capture output Function test ($ param) {if ($ param) {ob_start (); eval ($ param); $ contents = ob_get_contents (); ob_end_clean ();} else {echo 'Sorry, no output'; exit () ;}return $ contents ;} |