What is the difference between ob_flush and ob_end_flush?

I am confused about the PHP functions ob_flush() and ob_end_flush() . About the function ob_flush manual says

 The buffer contents are discarded after ob_flush() is called.This function does not destroy the output buffer like ob_end_flush() does. 

I got confused in the words discarded and destroyed here . Even if the contents of the buffer are discarded in the case of ob_flush() , they cannot be accessed, and even if they are destroyed, as in the case of ob_end_flush() , they cannot be accessed. Then what is the difference between these two functions?

UPDATE:

In response to JamWaffles answer I don’t understand the significance of deleting everything in the buffer, but keeping the buffer and deleting the whole buffer (freeing it up), because PHP does not have the concept of pointers, and you cannot get the address of the buffers, so it should not matter if you save empty buffer with you or free it.

+6
source share
3 answers

I think that in this case they mean the same thing. ob_flush() used when you want to clear parts of the page from the client, while ob_end_flush() clears the entire buffer and then destroys the buffer. What ob_flush() does is to delete everything in the buffer but save the buffer itself, so more data can be added after calling ob_flush() .


I will try to explain better.

Throw out

Say I have a beautiful, bright orange plastic bucket. This is my buffer . Then I get sand representing the contents of the buffer and filling the buffer (bucket) up. Then I select this bucket of sand and fill it in the sandbox, which is my client . You will notice that the sand is gone, but the bucket remains. This means that the buffer contents are discarded - the buffer itself can be reused (again filled with sand). In memory, the memory is freed but not freed, so it can be filled again.

Destroyed

Now, if we take our bucket again, fill it with sand again, pour out the sand and then set the bucket on fire, because we no longer require it, which is called buffer destruction ; the data in the buffer disappeared , but the buffer itself . In memory, memory is freed for another use.


How important is this in PHP, without pointers, the OP asks? Well, it depends on what you want to do. If you are processing a long page and want to (for example) send the title and sidebar to the client when you are processing the rest of the page to send after it, use ob_flush() .

If you want to dump something to the client without any results after it, use ob_end_flush() .


I mean absolutely disrespectful conversation in a rather patronizing tone; I wanted to make an analogy to make the definitions as clear as possible.

+16
source

ob_flush does not disable output buffering

+5
source

ob_end_flush() displays everything from the buffer and then destroys the buffer. ob_flush does the same, but does not destroy the buffer, just flushes it.

ob_flush() =

 ob_end_flush(); ob_start(); 
0
source

Source: https://habr.com/ru/post/896932/


All Articles