Why is this not output slowly?

Source: Internet
Author: User
But all output in 6 seconds


But all output in 6 seconds

Reply content:


But all output in 6 seconds

Under normal circumstances, all results will be returned to the browser after the PHP script is executed. If you want to return results in advance, you need to manually push the results. For example:

BubbleSort. php
  '. Str_repeat ('', 1024*4); ob_flush (); flush (); sleep (1) ;}echo 'done.'; ob_end_flush () ;?> Display 1 line every 1 second: array ([0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9) array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 0) array ([0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 1 [9] => 0) array ([0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 [7] => 2 [8] => 1 [9] => 0) array ([0] => 4 [1] => 5 [2] => 6 [3] => 7 [4] => 8 [5] => 9 [6] => 3 [7] => 2 [8] => 1 [9] => 0) array ([0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0) array ([0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0) array ([0] => 7 [1] => 8 [2] => 9 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0) array ([0] => 8 [1] => 9 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0) array ([0] => 9 [1] => 8 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0) done.

PHP_CLI_Server can output. Apache and Nginx gzip files in real time, which may cause the results generated by the flush () function to be not immediately sent to the client browser.
Pay attention to fastcgi buffer of Nginx under Nginx + PHP-FPM, for example:
Fastcgi_buffer_size 128 k;
Fastcgi_buffers 8 128 k;
It indicates that Nginx will buffer the information output by the PHP-FPM. When it reaches K, it will send the buffer data to the client, then we need to reduce the buffer first:
Fastcgi_buffer_size 4 k;
Fastcgi_buffers 8 4 k;
In addition, gzip must be disabled:
Gzip off;
Then, in php, before ob_flush and flush, output a piece of content that reaches 4 K. For example:
Echo str_repeat ('', 1024*4 );
By now, PHP can output the required content through ob_flush and flush row by row.

There are many problems with browser compatibility.
Since it is better to use js for output, you can obtain a message from the background every one second.

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.