difference between flush() and ob flush()

Please explain at last WHAT exactly these functions do (explain simply, I am a beginner) and what is the difference between them. I don't understand, for the life of me, the forums all use terminology that confuses those who have encountered them for the first time. And what else are the upper and lower layers of the buffer stack? What is the buffer stack? What are the layers? If someone explains this in an accessible way , it will help me a lot. Thank you so much in advance

Author: Muller, 2016-08-11

2 answers

If we explain in simple terms, when echo / print is called, the data is output (for example, the browser). In order not to drive through the byte with multiple echos, there is an output buffer where the output data is accumulated to a certain volume and when the data is overflowed, it is sent to the output. Then the data accumulates again. flush forces the contents of the buffer into the output stream.

For example, you have a long task where you need to constantly send messages to the browser results and you use echo "information". But they accumulate in the buffer and the browser will not get anything until the buffer is full. flush after echo forcibly sends the data. You can also disable the buffer, but in cli mode it does not exist at all.

Ob_* allows you to initialize your nested buffers by layering them on top of each other. And when clearing the data of such a buffer will fall into the underlying one up to the default one. And you can not do flush at all, but just pick up the accumulated result from the buffer and destroy it.

Ob_start opens a new buffer and all subsequent print/echo will fall into it. At any time, you can open a new buffer, or you can close and reset this buffer to a buffer that was opened earlier. That is, it looks like a layer cake (stack) of buffers, where data is written to the upper buffer and only when all buffers are flushed to the lower ones up to the default one, only then the data will not leave the output.

It is convenient to use this in a self-written template engine. For the template segment, we create a buffer and then take the finished html5 and destroy the buffer. Or when we call third-party code that abounds in echo and creates garbage - we wrap its call in ob_* and all this garbage settles there.

That is, flush resets the system buffer to output, and ob_flush resets last opened buffer via ob_start in underlying buffer (ob_* or system buffer)

 7
Author: vitidev, 2016-08-11 20:29:10

The answer is simple because ob_flush clears and outputs the buffer created by ob_start - 'output buffer start' and flush clears the output buffer of the script which sends its output to the browser usually this happens at the end of the script execution.

 1
Author: Naumov, 2016-08-11 16:50:40