Home > Article > Backend Development > Usage and differences between flush(), ob_flush(), and ob_end_flush() in PHP_PHP Tutorial
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
|
Copy code | ||||
echo str_repeat(" ",1024);