Home > Backend Development > PHP Tutorial > Buffering built-in functions in php_PHP tutorial

Buffering built-in functions in php_PHP tutorial

WBOY
Release: 2016-07-13 17:45:35
Original
925 people have browsed it


PHP output buffering (Output Buffering) means that PHP temporarily places all the content that is about to be sent to the browser in the buffer, and then outputs the content of the buffer after the entire PHP program is executed.


Almost all of the built-in functions related to buffering in PHP start with ob_. In addition, in the PHP configuration file php.ini, there is an output buffering instruction, which is usually turned off. We can manually turn it on when we use it (if it is turned on, buffering will be performed in all PHP programs).


When do we use buffering? It is generally used when setting sessions and cookies, because they cannot have output before, so the output buffering mechanism must be enabled.


1. Enable and end the buffering function - ob_start(), ob_end_flush()


When buffering is used outside, you can start buffering through ob_start(), and then end buffering with ob_end_flush(). Several commonly used functions:


ob_flush() - Send the buffer in advance, sending the output between ob_start() and ob_flush() to the browser in advance (before the entire php program ends).

ob_clean() - Clear the cache. The output buffer between ob_start() and ob_clean() will not be sent to the browser for display.

ob_end_clean() - clears the previous buffer like ob_clean(), the difference is: it will end the entire buffer function, and all output content after it will not be put into the buffer.

ob_start() can be used in multiple levels of nesting. It can open a large buffer and then clear a certain buffer as needed.


2. Use ob_start() to output the output to the buffer instead of to the browser.


Then use ob_get_contents to get the buffer data. ob_start() opens a buffer on the server to hold all output. So anytime echo is used, the output will be added to the buffer until the program ends or is terminated using ob_flush(). Then the content of the buffer in the server will be sent to the browser, which will be parsed and displayed by the browser.


At this time, the function ob_get_contents() must be used in front of ob_end_clean() to obtain the contents of the buffer.

In this case, the content can be saved to a variable before ob_end_clean() is executed, and then the variable can be operated after ob_end_clean().


ob_start();
echo 'How is the Communist Party of China?';
$name = ob_get_contents();
$length = ob_get_length();
ob_end_clean();

echo $name;


Author "wbgod_1987"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478663.htmlTechArticlePHP output buffering (Output Buffering) means that PHP temporarily places all content that is about to be sent to the browser. In the buffer, the contents of the buffer are output after the entire PHP program is executed...
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