PHP program code that outputs content in real time

Source: Internet
Author: User
If the real-time output is put in js, we can directly use settimeout for punctual input, which is very convenient, but this is not possible if it is implemented in php. next I will introduce the use of ob_flush () and the flush () function to achieve real-time transmission... if the real-time output is put in js, we can directly use settimeout for punctual input, which is very convenient, but this is not possible if it is implemented in php. next I will introduce the use of ob_flush () and the flush () function to implement real-time output.

In general, PHP sends the content to be output back to the client only after all the pages are executed. for example, the following code:

for ($i = 0; $i < 10; $i++) {     echo $i;     sleep(1); }

This code will output "0123456789" at a time in 10 seconds. for PHP programs that have been running for a long time, you may need to output the content in real time to view the running status. the code is as follows:

Header ("Content-type: text/html; charset = utf-8"); // when the execution time is set to an invalid value, the code is as follows: set_time_limit (0); // clear and disable the buffer, use this function before outputting data to the browser. the code is as follows: ob_end_clean (); // controls implicit buffering. the default value is off, the result of each print/echo or output command is sent to the browser. the code is as follows: ob_implicit_flush (1 );

The PHP output control functions ob_flush () and flush () are used. we modify the code to the following:

 

This code will immediately print Hello world on the screen. The key lies in the two functions ob_flush () and flush () called by lines 2nd and 3rd (), these two functions must be used together to ensure that the page outputs Hello world immediately, where str_repeat ('', 256) to solve this problem, some browsers must receive 256 characters before displaying the content.

The code for the above function upgrade is as follows:

 ";     sleep(1);     flush(); }?>

In this way, the page will output a number every second. we can easily use the PHP output control to display the page execution progress. However, due to the execution time limit on the PHP page, in addition, executing a page for a long time will put some pressure on the server.

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.