How PHP uses sleep to implement output--wait-and-output

Source: Internet
Author: User

The sleep () function is generally used on pauses, but once PHP has sleep, the rest of the output (Print,echo) waits for the sleep () function to complete, because the buffer is explained in detail here.

Do not repeat here, and if you want to achieve the first output and then wait for the output, then use the Ob_flush () and flush ();

The above linked article has such a paragraph explaining why it is necessary to Ob_flush () and flush ();

When the cache is not turned on, the contents of the script output are in the state of waiting for output on the server side, and flush () can send the content waiting for output to the client immediately.

When the cache is turned on, the contents of the script output are stored in the output cache, and there is no content waiting for the output state, and you directly use flush () without sending anything to the client. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client, you need to use Ob_flush () and then flush () before the client can immediately get the output of the script.

So the code is like this

<? PHP     Echo Date (' h:i:s '). ' </br> ';     Ob_flush ();     Flush ();      Sleep (5);     Echo Date (' h:i:s ');? >

How PHP uses sleep to implement output--wait-and-output

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.