Home >Backend Development >PHP Tutorial >PHP real-time output

PHP real-time output

WBOY
WBOYOriginal
2016-07-28 08:25:40912browse

Generally, PHP will output the content to be output to the page only after all tasks have been executed. For example, the following code:

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

This code will output "0123456789" in one go after 10 seconds ".

But sometimes we need to output the content immediately after executing the output function during the execution of the PHP page without waiting for the page to be fully executed. We modify the code to the following:

 PHP實時輸出

<span>set_time_limit</span>(0<span>);
</span><span>echo</span><span>str_pad</span>('',1024);<span>//</span><span>使用另一個字符串填充字符串為指定長度;</span><span>ob_implicit_flush</span>(<span>true</span>);<span>//</span><span>打開/關閉絕對發送;</span><span>for</span>(<span>$i</span>=1;<span>$i</span><10;<span>$i</span>++<span>){
    </span><span>echo</span><span>$i</span><span>;
    </span><span>sleep</span>(1<span>);
}</span>

 PHP實時輸出

In this way, the page will output a number every second.

We can easily use PHP’s output control to display the page execution progress. However, since PHP pages have execution time limits, and executing a page for a long time will put a certain amount of pressure on the server. So I use it appropriately.

The above has introduced PHP real-time output, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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