Home Backend Development PHP Tutorial php ob_start(), ob_end_flush and ob_end_clean() multi-level buffering_PHP tutorial

php ob_start(), ob_end_flush and ob_end_clean() multi-level buffering_PHP tutorial

Jul 13, 2016 pm 05:52 PM
clean end php start and yes buffer

ob_start() and ob_end_flush() are a good pair to control the output. There is no problem in understanding when they appear as a pair, but when they appear more often, it becomes more difficult to understand.


view sourceprint?01

02 ob_start();

03 echo 'level 1
';

04 ob_start();

05 echo 'level 2
';

06 ob_start();

07 echo 'level 3
';

08 ob_end_flush();

09 ob_end_flush();

10 ob_end_flush();


Obviously, the result is:

level 1
level 2
level 3

When the program is modified and ob_end_flush() is changed to ob_end_clean() to become the following, what do you think the result will be? Attached are the explanations of these functions:


ob_clean — Clear (erase) the output buffer
ob_end_clean — Clear (erase) the buffer and close output buffering
ob_end_flush — flush out (send out) the contents of the output buffer and close the buffer
ob_flush — Flush (send) the contents of the output buffer
ob_start — Open the output control buffer


view sourceprint?01

02 ob_start();

03 echo 'level 1
';

04 ob_start();

05 echo 'level 2
';

06 ob_start();

07 echo 'level 3
';

08 ob_end_clean();//Modifications

09 ob_end_flush();

10 ob_end_flush();


Result:

level 1
level 2

Maybe you think that ob_end_clean() will clear the output of its latest ob_start(); in fact, this statement is not very comprehensive. See the example below


view sourceprint?01

02 ob_start();

03 echo 'level 1
';

04 ob_start();

05 echo 'level 2
';

06 ob_start();

07 echo 'level 3
';

08 ob_end_clean(); //First modification

09 ob_end_flush();

10 ob_end_clean(); //Second modification


This time, nothing is output.

Isn’t there an ob_flush() in the middle? Logically speaking, it should output level2.

In fact, the main reason for this is the multi-level buffering mechanism of the output. This program example has three ob_start(), which means that it has three buffers A, B, and C. In fact, the php program itself also has a final output buffer, we will call it F.

In this program, these buffers have certain levels, C->B->A->F, F is the highest level and is the final output buffer of the program. We follow the above program. Give an explanation.


Just started. F:null


view sourceprint?1 ob_start();


Create a new buffer A. A: null -> F: null


view sourceprint?1 echo 'level 1
';


The program has output, and the output enters the lowest buffer A A: 'level 1
' -> F:null

view sourceprint?1 ob_start();

Create a new buffer B. B:null -> A: 'level 1
' -> F:null


view sourceprint?1 echo 'level 2
';


The program has output, and the output enters the lowest buffer B B:'level 2
' -> A: 'level 1
' ->F:null


view sourceprint?1 ob_start();


Create a new buffer C C:null B:'level 2
' A: 'level 1
' -> F:null


view sourceprint?1 echo 'level 3
';


The program has output, and the output enters the lowest buffer C C:'level 3
' -> B:'level 2
' -> A: 'level 1
' -> F:null


view sourceprint?1 ob_end_clean(); //First modification


Buffer C is emptied and closed. B:'level 2
' -> A: 'level 1
' -> F:null


view sourceprint?1 ob_end_flush();


Buffer B is output to the previous level buffer A and closed. A: 'level 1
level 2
' -> F:null

view sourceprint?1 ob_end_clean(); //Second modification www.2cto.com

Buffer A is cleared and closed. At this time, the contents of buffer A have not been actually output to the final F, so the entire program does not have any output.

ob There are many other functions, but as long as you can understand these mechanisms, it should not be difficult to understand. Attach the remaining functions


flush — Flush the output buffer
ob_clean — Clear (erase) the output buffer
ob_end_clean — Clear (erase) the buffer and close output buffering
ob_end_flush — flush out (send out) the contents of the output buffer and close the buffer
ob_flush — Flush (send) the contents of the output buffer
ob_get_clean — Get the contents of the current buffer and delete the current output buffer.
ob_get_contents — Returns the contents of the output buffer
ob_get_flush — Flushes (sends out) the contents of a buffer, returns the contents as a string, and closes the output buffer.
ob_get_length — Returns the length of the output buffer contents
ob_get_level — Returns the nesting level of the output buffering mechanism
ob_get_status — Get the status of all output buffers
ob_gzhandler — The callback function used in ob_start to compress the contents of the output buffer. ob_start callback function to gzip output buffer
ob_implicit_flush — Turn on/off absolute flushing
ob_list_handlers — List all output handlers in use.
ob_start — Open the output control buffer
output_add_rewrite_var — Add URL rewriter values ​​
output_reset_rewrite_vars — Reset URL rewriter values ​​


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478080.htmlTechArticleob_start() and ob_end_flush() are a good pair to control the output. There is no problem in understanding them when they appear as a pair, but when the two of them appear more often...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles