Let's talk about output_buffering and phpoutputbuffering in PHP. Let's talk about output_buffering and phpoutputbuffering in PHP. I. What are the cache in php! In PHP, we can roughly divide the cache into client cache (Browse talk about output_buffering and phpoutputbuffering in PHP ).
I. Let's talk about the cache in php!
In PHP, we can roughly divide the cache into Browser cache and Server cache ). Because PHP is based on the B/S architecture, we can understand it as browser-side caching and server-side caching.
In the cache provided by PHP on the server side, there are two main types of cache! Program cache and OB cache! This is also the main content for us to learn about server cache!
The cache output sequence in PHP is:
Php output cache is enabled: echo, print-> php output_buffering-> server buffering-> browser display
Php output cache not opened: echo, print-> server buffering-> browser display
Browser output cache: IE is 256 Bytes, Chrome and FireFox are 1000 Bytes. the browser outputs the data on the page only when the output data reaches this length or the script ends.
2. server response process
A. The client sends A request response to the server!
B. The Apache server loads the PHP module and starts the corresponding process (or thread) to run the corresponding PHP script page!
C. If the OB cache is not enabled, all running results will be put into the program cache, and then packaged and sent to the browser! The browser renders the page to generate the WEB page we finally see!
D. When OB cache is enabled, the running results will be put into OB cache and program cache respectively. when the program runs to the last row, the data in the OB cache will be refreshed back to the program cache, and then packaged and returned to the browser! The browser renders the page to generate the WEB page we see!
III. common usage of OB cache!
A. output_buffering = 4096, with less data output (less than one buffer)
for($i=0; $i<5; $i++){ echo $i.'
'; sleep(1); }
Running result: it is output only after all the scripts are completed, because the data size is below the buffer size.
B. output_buffering = 4096, output less data (less than one buffer), disable output_buffering, and modify output_buffering of php. ini = 0.
Echo str_repeat ("", 1024); // A blank for ($ I = 0; $ I <5; $ I ++) {echo $ I ."
"; Flush (); sleep (1 );}
Running result: if OB is disabled, you do not need to wait until the script is run. if the data is not in OB, you can see intermittent output. Echo-> browser buffering-> browser display
C. output_buffering = 4096. output large data (greater than one buffer) without using ob_start ()
for($i=0; $i<5; $i++){ echo file_get_contents('f.txt').$i.'
'; sleep(2); }
Running result: f.txt is a file larger than 4 kB. because it is greater than the default value of buffer, the buffer space is insufficient. every time a buffer is full, it will be output, so intermittent output can be seen.
D. output_buffering = 4096. output large data (greater than one buffer). use ob_start ()
ob_start(); for($i=0; $i<5; $i++){ echo file_get_contents('f.txt').$i.'
'; sleep(2); }
Running result: because ob_start () is used, enough space is set for the buffer, so it will be saved to the output after the script is executed.
E, output_buffering = On, use ob_start ()
ob_start();echo "abc-";header("content-type:text/html;charset=utf-8");echo "hello-";ob_end_flush();echo "aa-";echo ob_get_contents();
Running result: abc-hello-aa-
F, output_buffering = Off, use ob_start ()
ob_start();echo "abc-";header("content-type:text/html;charset=utf-8");echo "hello-";ob_end_flush();echo "aa-";echo ob_get_contents();
Running result: abc-hello-aa-
The output buffer is stackable, which means that when an ob_start () is active, you can call another ob_start (). Make sure that you have correctly called ob_end_flush () the appropriate number of times. If multiple output callback functions are active, the output content will be filtered in the nested order.
Note: in PHP5.2, OB is disabled by default, and it is enabled by default after 5.3;
Common methods:
1. ob_start
Activate the output_buffering mechanism. Once activated, the script is not directly output to the browser, but is temporarily written to the php buffering area. It is not sent until the script is run.
2. ob_get_contents
To obtain data in php buffering, note: it must be called before ob_end_clean (). Otherwise, only null characters are obtained.
3. ob_end_flush and ob_end_clean
Ob_end_flush will output data in php buffering, but will not be cleared.
Ob_end_clean does not output data, but only clears data in php buffering.
4. ob_flush, flush, ob_implicit_flush
Ob_flush refreshes the data in php buffering to the program cache.
Flush will refresh the program and cache it to the browser cache.
Ob_implicit_flush will open or close the absolute (implicit) flush. Absolute (implicit) sending will cause a sending operation after each output call, so that explicit calling of flush () is no longer needed.
IV. Role of OB cache!
OB cache is applied in all aspects, but I know it mainly in two aspects!
A. selecting OB cache is a good choice when the website is static!
B. Solve the error of Warning: Cannot modify header information-headers already sent!
The cause of the error is that the response header and the corresponding body location are misplaced! Normally, the content that the server returns to the browser should be: response header + response body!
However, if OB cache is enabled, the corresponding header information (usually the information set by the header () function) will be put into the program cache!
Other output content, such as echo print_r var_dump, will be put into the OB cache first!
When the program ends, or the OB cache is closed, put the OB cached content into the program cache! This ensures that the response header information is always before the response body content!
The above is all the content of this article. I hope you will like it.
Tips 1. what are the cache in php! In PHP, we can roughly divide the cache into client cache (Browse...