Home  >  Article  >  Backend Development  >  Usage and differences between flush(), ob_flush(), and ob_end_flush() in PHP_PHP Tutorial

Usage and differences between flush(), ob_flush(), and ob_end_flush() in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:53860browse

This article will summarize and introduce to you the usage and differences of flush(), ob_flush(), and ob_end_flush() in php. Friends who need to know more can enter for reference.

First of all, let’s talk about buffer. It is a memory address space, which is 4096 (1kb) [find the output_buffering configuration in the php.ini configuration file]. PHP has a php output_buffering mechanism. When the PHP code is executed, it is not immediately Output the content, but want to output the echo/print content to the buffer. When the buffer is full, the data will be handed over to the system kernel and passed to tcp to the browser for display. When the php php output_buffering mechanism is turned on (the default is turned on, you can After being turned on through the ob_start() function), only when the data in the php buffer reaches the set value, will the data in the buffer be sent to the browser. But browsers also have cache. Some versions of browsing only output content when the data reaches 256 bytes,
ob_start() function: Open the output buffer.
Function format void ob_start(void)
Note: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.

Flush: refresh the contents of the buffer and output.
Function format: flush()
​ Description: This function is frequently used and is very efficient.

ob_get_contents: Returns the contents of the internal buffer.
Function format: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.

ob_get_length: Returns the length of the internal buffer.
Function format: int ob_get_length(void)
Description: This function will return the length in the current buffer; like ob_get_contents, if the output buffer is not activated, it will return FALSE.

ob_end_clean: delete the contents of the internal buffer and close the internal buffer
Function format: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it

ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer
Function format: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any)

ob_implicit_flush: Turn absolute flush on or off
Function format: void ob_implicit_flush ([int flag])
Note: The buffer is turned off by default. After absolute output is turned on, each script output is sent directly to the browser, and there is no need to call flush()


flush() can immediately send the content waiting for output to the client, while ob_flush() will only output when the buffer is full. You can verify it through the following simple PHP example:

Example

The code is as follows
 代码如下 复制代码

//这是防止浏览器的缓存
echo str_repeat(" ",1024);
for($i=0;$i<5;$i++){
echo $i;
sleep(1);
flush();//会每隔1s输出一个数字,但是使用ob_flush()会等待5s一起输出
}
?>

Copy code
//This is to prevent browser caching

echo str_repeat(" ",1024);

for($i=0;$i<5;$i++){ sleep(1); Flush();//will output a number every 1s, but using ob_flush() will wait for 5s to output together } ?>
http://www.bkjia.com/PHPjc/633070.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633070.htmlTechArticleThis article will summarize and introduce to you the usage and differences of flush(), ob_flush(), and ob_end_flush() in php , friends who need to know can enter for reference. First let’s talk about buffer, it is a...
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