Output Control in PHP

Source: Internet
Author: User
Tags flush sleep

Buffered output

<?php
for ($i =10; $i >0; $i-)
{
echo $i;
Flush ();
Sleep (1);
}
?> according to the PHP manual

This function sends all the output of the program so far to the user's browser.

The above code, should be output every second $i. But this is not necessarily the case in practice. It is possible to wait for 10 seconds after all the output is presented at the same time.

OK, let's change this code and change it to

<?php
Ob_end_clean ()//Modify part
for ($i =10; $i >0; $i-)
{
echo $i;
Flush ();
Sleep (1);
}
?> Hey, add this sentence Ob_end_clean (), incredibly ok. In fact, we change the Ob_end_clean () to Ob_end_flush () is also OK.

I'll change it again.

<?php
for ($i =10; $i >0; $i-)
{
echo $i;
Ob_flush ()//Modify part
Flush ();
Sleep (1);
}
?> run a bit, is not found $i also every second output once? Why is that?
Don't worry, let's take a look at php.ini.

Open php.ini, search output_buffering, and we'll see settings like this output_buffering = 4096. As its name output_buffering, the effect of this setting is to buffer the output, with a buffer size of 4096bytes.

In our first code, the output is not as expected because the output_buffering buffers the output. The output will not be sent out until the 4096bytes is reached or the script is finished.

The function of Ob_end_clean () and Ob_end_flush () in the second code is to terminate the buffer. This doesn't have to wait until a 4096bytes buffer is sent out.

In the third code, a Ob_flush () is used to send the buffered data, but it does not terminate the buffer, so it must be used before each flush ().

If you do not want to use Ob_end_clean (), Ob_end_flush () and Ob_flush (), we must set the output_buffering in php.ini small enough, for example, to 0. Note that if you're going to use Ini_set ("output_buffering", "0″") in your script, stop it, this method doesn't work. Because at the beginning of the script, the buffering settings are loaded, and the buffer begins.

Perhaps you will ask, since Ob_flush () is to send the buffered data out, then why need to use flush ()??? Can't you just use the code below??

<?php
for ($i =10; $i >0; $i-)
{
echo $i;
Ob_flush ();
Sleep (1);
}
?> Please note the difference between ob_flush () and flush (). The former releases data from the PHP buffer, which sends the data that is not in the buffer or is released to the browser. So when buffering exists, we have to Ob_flush () and flush () at the same time.

Is that not flush () here is indispensable? No, we have another way to get it out to the browser right away when there's a data output. The following two pieces of code do not need to use flush (). (When you set the output_buffering to 0, Ob_flush () and Ob_end_clean () are not needed)

<?php
Ob_implicit_flush (true);
for ($i =10; $i >0; $i-)
{
 echo $i;
 ob_flush (); br>  sleep (1);
}
? ><?php
Ob_end_clean ();
Ob_implicit_flush (true);
for ($i =10; $i >0; $i-)
{
 echo $i;
 sleep (1);
}
?> Note the above Ob_implicit_flush (true), which forces the output to be immediately sent to the browser whenever there is an output. This does not require each output (echo) to be sent to the browser with flush ().

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.