I ask you, finally explain what exactly these functions do (explain simply, I'm a beginner) and what is the difference between them. I do not understand, for the life of me, on the forums everyone uses terminology that confuses those who encountered them for the first time. And what else for the top and bottom layers of the buffer stack? What is the stack of buffers? What are the layers? If someone explains it easily, it will help me a lot. Thank you very much in advance
- stackoverflow.com/questions/4191385/… - Qwertiy ♦
2 answers
If explained in simple terms, when echo / print is called, the data goes to the output (for example, a browser). So that with numerous echo not to drive on a byte there is an output buffer, where the output data accumulate to a certain volume and when overflow the data is sent to the output. Then again there is an accumulation of data. flush forces the contents of the buffer to the output stream.
For example, you have a long task, where you need to constantly send results to the browser and you use echo "information". But they accumulate in the buffer and the browser will not get anything until the buffer overflows. flush after echo forcibly sends data. Also, the buffer can be disabled, and in cli mode it does not exist at all.
ob_ * allows you to initialize your nested buffers by layering them on each other. And when clearing the data of such a buffer will fall into the underlying up to the default. And you can never do flush, but simply 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 the data to the buffer opened earlier. That is, it looks like a puff pie (stack) of buffers, where the data is written to the upper buffer and only when all buffers are dropped to the underlying ones up to the default one, only then the data will not be output.
It is convenient to use it in samopisny template. For the template segment, create a buffer and then pick up the finished html and destroy the buffer. Or when we call a third-party code that abounds in echo and creates garbage - we wrap its call in ob_ * and all this garbage accumulates there.
that is, flush flushes the system buffer to the output, and ob_flush flushes the last open buffer through ob_start to the underlying buffer (ob_ * or system)
- Why did they need both, and even in this order? - Qwertiy ♦
- Excellent. Thank you very much. - Muller
- @Qwertiy I do not know, ob_ is superfluous if he has not started anywhere. And so it should be remembered that in addition to the output buffer in php data will be deposited in the server buffer. Because of this, you have to configure the server in a special way, but flush is enough. (And even mono do @ini_set ('implicit_flush', 1);) - vitidev
- Another commentary says that just flush is not enough. - Qwertiy ♦
- flush is not easy enough. I checked it myself yesterday. First flush, then ob_flush, then the effect is - Muller
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. This usually happens when the script ends.