Home > Article > Backend Development > PHP cannot output principle analysis every second using ob_flush
The example in this article describes the principle that PHP cannot output every second using ob_flush. Share it with everyone for your reference. The specific analysis is as follows:
Function implementation:
The browser outputs a number every second.
php.ini is configured as:
Version 5.3
implicit_flush = off
output_buffering = off
Another: Check whether output_buffering is turned on, you can:
var_dump(ini_get('output_buffering'));
Okay, let’s take a look at this code again:
<?php $i = 3; ob_start(); while ($i--) { echo $i, "<br />"; ob_flush(); flush(); sleep(1); } ob_end_clean(); ?>
But why: This code cannot be output every second? ?
Cause analysis:
apache operating principle: when you access an address (send a request), apache starts PHP, then PHP execution is at the page level, that is, if there is executable code: it is all executed before throwing it to apache, apache then throws it to the browser to display the results
How to achieve this?
If the cli displays the results in a different way, what’s the difference?
linux cmd:
php5 test.php
executed directly by php without going through apache or web service, you can achieve:
<?php $i = 3; while ($i--) { echo $i, "\n"; sleep(1); } ob_end_clean(); ?>
I hope this article will be helpful to everyone’s php programming design.
For more relevant articles on the principle analysis of PHP using ob_flush, which cannot output every second, please pay attention to the PHP Chinese website!