PHP flush () refresh cannot output buffering reason analysis

Source: Internet
Author: User

In the PHP program writing, the usage offlush () is still very high, it plays a very important role in the Web page performance of instant information, such as the previous written PHP implementation limit file download speed code instance , flush () Plays a pivotal role, is the key statement in the implementation code of the progress bar.

Explanation of Flash ()

Grammar:

Flush ();

Role:

Refreshes the PHP program's buffering, regardless of the circumstances in which PHP executes (CGI, Web server, etc.). This function sends all the output of the program so far to the user's browser.

Problem:

Flush () Refresh why can't I output buffering?

Very simple program code

for ($i =10; $i >2; $i--)
{
echo $i. ' <br/> ';
Flush ();
Sleep (1);
}

The result should be from 10 to 3, then output to the browser, as follows:

The above code, if there is no flush (), this sentence, 10 to 3 of the number will be output to the browser together.

However, this is the code above, I was successful in the local test, but uploaded into the space after the invalid, what is the matter?

Later I looked up the relevant information, found that flush (), the use of the need to pay attention to some problems, these problems may not be encountered when we do not think of it. That is, flush () needs to be used in conjunction with Ob_flush (), otherwise no buffering will be output in the Linux system.

and flush () and Ob_flush () with the use of the same time, the wording also need to pay attention to, that is to write Ob_flush (); write flush ();, as follows:

Ob_flush ();
Flush ();

Well, the symptom of the problem is here, so the above code should be written as follows:

for ($i =10; $i >2; $i--)
{
echo $i. ' <br/> ';
Ob_flush (); This sentence must not be less
Flush ();
Sleep (1);
}
Ob_end_flush ();

In this way, no matter which system, which Web server, can be the normal output buffering.

PHP flush () refresh cannot output buffering reason analysis

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.