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.