Home > Backend Development > PHP Tutorial > PHP uses ob_flush to step into pit analysis

PHP uses ob_flush to step into pit analysis

*文
Release: 2023-03-18 15:44:01
Original
1365 people have browsed it

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
Copy after login

Also: check whether output_buffering is turned on, you can:

var_dump(ini_get('output_buffering'));
Copy after login

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();
?>
Copy after login


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
Copy after login

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();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template