How PHP executes time-consuming scripts to output content in real time

藏色散人
Release: 2023-04-07 22:34:02
forward
2630 people have browsed it

We sometimes have such a requirement. When executing a time-consuming script, the data will be output only after the script is executed. If it is not processed, it will cache the content output to the page into the memory until it is processed. It will be executed later. Here, we need real-time output.

flush — Flush the output buffer

Flushes the buffer of the PHP program, regardless of the circumstances under which PHP is executed (CGI, web server, etc.). This function sends all the program's output so far to the user's browser.

Theflush() function has no effect on the cache mode of the server or client browser. Therefore, both the ob_flush() and flush() functions must be used to flush the output buffer.

Individual web server programs, especially web server programs under Win32, will still cache the output of the script until the end of the program before sending the results to the browser.

Some Apache modules, such as mod_gzip, may perform output caching themselves, which will cause the results generated by the flush() function to not be sent to the client browser immediately.

Even the browser will cache the received content before displaying it. For example, the Netscape browser caches content until it receives a line break or the beginning of an html tag, and does not display the entire table until it receives a tag.

Some versions of Microsoft Internet Explorer will only begin to display the page after receiving 256 bytes, so some additional spaces must be sent to allow these browsers to display the page content.

If real-time output is required, two core functions ob_flush, flush;

<?php
 
 // echo phpinfo();
 
if (ob_get_level() == 0) ob_start();
 
for ($i = 0; $i<10; $i++){
 
    echo "<br> Line to show.";
    echo str_pad(&#39;&#39;,4096)."\n";
 
    ob_flush();
    flush();
    sleep(2);
}
 
echo "Done.";
 
ob_end_flush();
Copy after login

For more PHP-related knowledge, please visit PHP tutorial!

The above is the detailed content of How PHP executes time-consuming scripts to output content in real time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!