Output Control (Output Control) in PHP
Introduction to php Buffer
In fact, I still have a vague impression on the php ob series, but I still don't know much about how to play it. curd does not go deep into these contents at ordinary times. As a phper, I am very ashamed. I searched the internet and copied each other. The code run cannot be described by the author. This article is produced by conscience and the code has been run by the author.
When the output is executed, such as echo and print. The output is not sent to the web server immediately. Instead, the data is written to the php buffer. The advantages of the php output_buffering mechanism certainly improve performance. In fact, the php file is finally displayed on the browser and goes through three buffer stages: php buffer = "web server buffer =" browser buffer. Finally displayed in the browser
By default, php buffer is enabled, and the default value of this buffer is 4096, that is, 4 kb. You can find the output_buffering configuration in the php. ini configuration file. Buffer is a memory address space. The default size of a Linux system is generally 4096 (4 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. When you open a text editor and edit a file, the operating system does not immediately write this character to the disk for each input, instead, it is written to the buffer first. When the buffer is full, the data in the buffer is written to the disk. Of course, when the kernel function flush () is called, the dirty data in the buffer must be written back to the disk.
For example
<? Phpecho "nanwu Amitabha <br>"; header ("content-type: text/html; charset = 'utf-8'"); echo "really cool! "; // Output // nanwu Amitabha // really cool
Header () must be called before any actual output, but our program has been output but runs normally. Check the following code:
<? Phpecho "nanwu Amitabha <br>"; ob_flush (); header ("content-type: text/html; charset = 'utf-8'"); echo "really cool! "; // Output // Amitabha in South China // Cannot modify header information-headers already sent by (output started at E: \ php \ test. php: 3) // really cool
The above program indicates that the program is not output immediately, but the buffer and output are refreshed only when the ob_flush function is called.
Ob_flush () and flush ()
The ob_flush () and flush () function php manuals are described in detail. You can check them out. The difference between the two is:
Ob_flush () is the buffer for refreshing PHP itself
Flush () is the buffer used to refresh the WebServer server. Output to the browser.However, the following situations may occur:
- Some web server programs, especially the web server programs under Win32, will cache the Script output until the program ends before sending the results to the browser.
- Some Apache modules, such as mod_gzip, May output the cache by themselves, which will cause the results produced by the flush () function to be not immediately sent to the client browser.
- Even the browser caches the received content before it is displayed. For example, the Netscape Browser caches content before it receives a line break or the beginning of an html Tag, and does not display the entire table until it receives the </table> tag.
- In some versions of Microsoft Internet Explorer, the page is displayed only after 256 bytes are received. Therefore, some extra spaces must be sent for these browsers to display the page content.
For example:
<? Php/** Server: LightTPD/1.4.28 (Win32) X-Powered-By: PHP/5.3.27 */echo 'ffa border '. "<BR>"; ob_flush (); flush (); sleep (1); echo 'convert normally '; // output
The code above is a line of output in the chrome browser, and all output in the ie series browser. In fact, in the fourth article above, some browsers start to display only when they receive 256 characters. Change the code above to the following format:
<? Php/** Server: LightTPD/1.4.28 (Win32) X-Powered-By: PHP/5.3.27 */echo str_pad ('', 240 ). "\ n"; echo 'ffa boundless '. "<BR>"; ob_flush (); flush (); sleep (1); echo 'convert normally '; // output
In this way, a line of output is displayed under ie, because it contains more than 256 characters.
Description of other ob Functions
1. ob_end_flush and ob_end_clean
As the name suggests, end ends. When the buffer is closed, the output buffer is closed, and the output buffer is cleared. For example
<?php/**Server:LightTPD/1.4.28 (Win32)X-Powered-By:PHP/5.3.27*/echo 'before';ob_end_clean();echo str_pad('',4096)."\n";for ($i=10; $i>0; $i--) { echo $i; sleep(1);}
The above code is to output all the content, rather than output one by one. Ob_end_clean () is the buffer disabled? Why not output one by one? As we mentioned above, php is not directly output to the browser, but to the web server. Although php has no buffer. However, there are still some web servers. So the following code:
/**Server:LightTPD/1.4.28 (Win32)X-Powered-By:PHP/5.3.27*/echo 'before';ob_end_clean();echo str_pad('',4096)."\n";for ($i=10; $i>0; $i--) { flush(); echo $i; sleep(1);}
With flush (), a row of output is displayed. If you replace ob_end_clean with ob_end_flush, before is output.
For more information about other functions, see the manual.
Summary
Php scripts must go through php buffer = web server buffer = browser buffer. Finally, it is displayed in the browser. Indispensable. So we need ob_flush and flush and echo str_pad ('', 4096) to debug the desired effect.
The above is to sort out the PHP buffer information and continue to add relevant information. Thank you for your support for this site!