PHP Output buffer controls (output control) detailed _php tips

Source: Internet
Author: User
Tags flush php script win32

Introduction to PHP Buffering

In fact, my impression of the PHP OB series is still very vague, specific how to play, is not very understanding, peacetime curd, do not go into these content. As Phper is very ashamed. Online Search a pass, mutual copy, the code can not be run by the author described the phenomenon, this article conscience production, the code is the author has run.

When performing the output, such as Echo,print. The output is not sent to the Web server immediately, but rather writes the data to PHP buffer. PHP output_buffering mechanism benefits certainly improve performance. In fact, the PHP file is eventually displayed in the browser, through 3 buffer stage: PHP buffer= "Web server buffer=" browser buffer. Finally show to browser

By default, PHP buffer is turned on, and the buffer default is 4096, or 4 KB. You can find the output_buffering configuration in the php.ini configuration file. Buffer is a memory address space, the Linux system default size is generally 4096 (4KB), that is, a memory page. It is primarily used for data transfer between devices that are not synchronized at the storage speed or between devices with different priority levels. By using buffer, the process can wait for each other to get smaller. Here's a popular example, when you open a text editor to edit a file, you input a character, the operating system will not immediately write this character directly to the disk, but first write to the buffer, when the full of a buffer, the data in the buffer will be written to disk, Of course, when calling kernel function flush (), it is mandatory to write the dirty data in the buffer back to disk.

As an example

<?php
echo "South no Amitabha <br>";
Header ("content-type:text/html;charset= ' Utf-8 '");
echo "Really good patience!";
Output//
South no Amitabha
//really good patience

header () must be called before any actual output, but our program has been exported, but it works. Looking at the following code:

<?phpecho "South no Amitabha <br>";
Ob_flush ();
Header ("content-type:text/html;charset= ' Utf-8 '");
echo "Really good patience!";
Output
//south without Amitabha
//cannot Modify header information-headers already sent by (output started at E:\php\test.ph P:3)
//really good patience

The above program shows that the program does not immediately output, and when the Ob_flush function is called to refresh the buffer, output.

Ob_flush () and flush ()

Ob_flush (), the Flush () PHP manual has detailed instructions, you can check it out. The difference between the two is:

Ob_flush () is the buffer that refreshes PHP itself

Flush () is the buffer that it is refreshing the webserver server. Output to the browser. However, the following conditions will occur:

    1. Individual Web server programs, especially Web server programs under Win32, still cache the output of the script until the end of the program before sending the results to the browser.
    2. Some Apache modules, such as Mod_gzip, may have their own output caching, which will result in the results of the flush () function not being immediately sent to the client browser.
    3. Even browsers will cache what they receive before it is displayed. For example, Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the </table> tag is accepted.
    4. Some versions of Microsoft Internet Explorer will only start displaying the page after the 256 bytes received, so you must send some extra spaces to allow the browsers to display the page content.

Like what:

<?php
/**
server:lighttpd/1.4.28 (Win32)
x-powered-by:php/5.3.27
/
echo ' Dharma boundless '. <BR> ";
Ob_flush ();
Flush ();
Sleep (1);
Echo ' Faling ';

Output

The above code in the Chrome browser is a line of output, in the IE series browser is all output. It's actually the fourth one. Some browsers only start to display when they receive 256 characters. Change the above code to the following form:

<?php
/**
server:lighttpd/1.4.28 (Win32)
x-powered-by:php/5.3.27
*/
Echo str_pad (", 240). " \ n "; 
Echo ' The Dharma is boundless '. <BR> ";
Ob_flush ();
Flush ();
Sleep (1);
Echo ' Faling ';

Output

This will be one line of output below ie, because it is more than 256 characters.

OB Other function description

1.ob_end_flush and Ob_end_clean

End as the name implies, close the buffer, are off the output buffer, one is the output buffer, one is clear. Like what

<?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 the output of the entire content, rather than one output. Ob_end_clean () not off the buffer? How is not an output, in fact, we said above, PHP is not directly output to the browser, but the Web server. Although PHP does not have a buffer. But the Web server still exists. 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);
}

Plus flush (), a line of output. If you change the Ob_end_clean to Ob_end_flush, you will before output.

Other functions can refer to the manual, relatively simple.

Summarize

PHP script to browser, to go through the PHP buffer= "Web server buffer=" browser buffer. Finally, the browser is displayed. Indispensable. So we're going to ob_flush and flush and add echo str_pad (", 4096") to debug the effect you want.

The above is the PHP buffer data collation follow-up to continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.