Home  >  Article  >  Backend Development  >  What is PHP’s output buffering?

What is PHP’s output buffering?

青灯夜游
青灯夜游Original
2019-01-10 13:41:423612browse

Output buffering is a way of telling PHP to save certain data before sending it to the browser. It can be used to freely control the output of data in the program, where the control output will be based on the time of sending the output to the browser, the output sequence, etc. Let's take you through PHP's output buffering, hoping it will be helpful to you.

What is PHP’s output buffering?

By default, using PHP, after generating HTML by executing a statement, it is sent to the browser as a data block immediately. Whereas, with PHP output buffering, the generated HTML will be stored in a buffer or variable and sent to the buffer for rendering after the last statement in the PHP script is executed. This is a significant improvement in performance and adds aesthetic value to web pages. [Course Recommendation: PHP Tutorial]

Advantages of PHP Output Buffering

The following are some advantages of using output buffering :

1. When output buffering is enabled, developers reduce the number of interactions between the server and the client browser because the entire HTML is sent at once, so for larger projects, output buffering provides greater savings. time method.

2. Since the buffer stores the entire HTML as a string, we can use all string methods or custom methods to manipulate HTML, providing greater flexibility when rendering content.

3. Output buffering can provide faster, safer, more flexible, and less redundant rendering methods; we can also apply various compression methods to create more efficient rendering.

4. Using output buffering makes it easier to set cookies and use sessions because the rest of the page's content is not included when sending header information.

Common functions of PHP output buffering:

The following are some common output buffering functions:

ob_start() function : Turn on output buffering. In other words, it creates a buffer (invisible holding cell) which will store all output after the call.

ob_get_contents() function: grab all the data collected after we call ob_start, that is, all the contents in the buffer. Normally you assign it to a variable.

ob_clean() function: Remove all content from the output buffer. Note that it doesn't output anything.

ob_flush() function: Output the content in the buffer. Note that it does not erase the buffer.

ob_end_clean() function: Basically runs ob_get_contents(), erases the buffer, and turns off output buffering.

ob_end_flush() function: Output content from the buffer and end the output buffer. It does not erase the buffer.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is PHP’s output buffering?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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