PHP output buffer (OutputBuffering) means that PHP puts all the content that will be sent to the browser in the buffer temporarily. after the entire PHP program is executed, it outputs the content in the cache.
PHP Output buffer (Output Buffering) means that PHP puts all the content that is about to be sent to the browser in the buffer temporarily, and outputs the content in the cache area after the entire PHP program is executed.
In PHP, buffer-related built-in functions are almost all started with ob. In addition, in the PHP configuration file php. ini contains the output buffering command, which is usually disabled. we can manually enable it when using it (if enabled, it will be executed in all php programs ).
When should we use buffering? It is usually used when setting sessions and cookies, because they cannot have output before, and all should enable the output buffer mechanism.
1. Enable and end the buffer functions-ob_start () and ob_end_flush ()
When buffering is used outside, you can enable the buffering through ob_start (), end the buffering, and use ob_end_flush (). Several common functions:
Ob_flush () -- sends the buffer in advance, and sends the output between ob_start () and ob_flush () to the browser before the end of the entire php program.
Ob_clean () -- clears 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 that it will end the entire buffer function, and all output content after it will not be placed in the buffer.
Ob_start () can be nested at multiple levels. you can enable a large buffer, and then clear a buffer as needed.
II. obtain the buffer content-ob_get_contents ()
We want to get some output, but don't want it to be displayed in the browser. we can get all the Buffered content first and then clear the cache. For example:
Ob_start ();
Echo 'luchanghong ';
$ Name = ob_get_contents ();
$ Length = ob_get_length ();
Ob_end_clean ();
Then we can get the content of the output buffer, which is saved in $ name. In addition, ob_get_lehgth () can get the length of the output buffer.
The fetch method in the smarty template uses ob_get_contents () for implementation.