Home >Backend Development >PHP Tutorial >PHP uses ob_flush to step into pit analysis
This article mainly introduces the principle that PHP cannot output every second when using ob_flush. It also analyzes in detail the related principles of PHP using ob_flush and the method of using CLI under Linux. I hope to be helpful.
The specific analysis is as follows:
Implementation function:
The browser outputs a number every second.
php.ini is configured as:
Version 5.3
implicit_flush = off output_buffering = off
Also: check whether output_buffering is turned on, you can:
var_dump(ini_get('output_buffering'));
Okay, let’s take a look at this paragraph again Code:
<?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: after it is fully executed, it is thrown to apache, and apache is then thrown to the browser to display the results.
How to achieve this?
If the cli displays the results in a different way, where is 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(); ?>
Related recommendations:
Introduction to PHP ob cache and detailed explanation of ob function
PHP ob cache analysis and understanding
In-depth explanation of the usage of php ob_start
The above is the detailed content of PHP uses ob_flush to step into pit analysis. For more information, please follow other related articles on the PHP Chinese website!