PHP: Detailed explanation of the usage differences between flush() and ob_flush(), ob_end_flush()

伊谢尔伦
Release: 2023-03-11 10:14:01
Original
3611 people have browsed it

First let’s talk about buffer. It is a memory address space, which is 4096 (1kb) [Find the output_buffering configuration in php.iniConfiguration file]. PHP has a php output_buffering mechanism. PHP code is executing When the content is output, the content is not output immediately, but the echo/print content is output to the buffer. When the buffer is full, the data will be handed over to the system kernel and passed to the tcp to the browser for display. When the php php output_buffering mechanism is turned on ( It is enabled by default and can be enabled 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 the browser also has a 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: Return 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 words)

ob_implicit_flush: Turn on or off absolute flush
Function format: void ob_implicit_flush ([int flag])
Description: The default is to close the buffer, after turning on absolute output , 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() can only wait for It will only output when the buffer is full. You can verify it through the following simple PHP example:

Sample code:

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

The above is the detailed content of PHP: Detailed explanation of the usage differences between flush() and ob_flush(), ob_end_flush(). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!