Home >Backend Development >PHP Tutorial >PHP cannot output principle analysis every second when using ob_flush, phpob_flush_PHP tutorial
The example in this article tells that php cannot output principle every second when 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:
Copy code The code is as follows: 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 will be thrown to apache after it is all executed, and apache will Throw it to the browser to display the results
How to achieve it?
If the cli displays the results in a different way, what’s the difference?
linux cmd:
php5 test.php
It can be implemented directly by php without going through apache or web service:
<?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.