What ob_start and ob_gzhandler Really Do

I know ob_start turns on output buffering, but I don't quite understand what that means. For me, this means that it just stops the script outputting data.

It's true? How does the browser output data in this case, do I need to use ob_end_flush () to turn it off at the end?

Since ob_gzhandler compresses web pages, how do browsers render those pages?

I saw ob_start ("gzhandler") in the code, since ob_gzhandler compresses web pages, what does ob_start ("gzhandler") mean and how does it apply to both functions?

All help is appreciated!


source to share

3 answers

Output buffering means that instead of writing your output directly to the stdout stream, it is written to the buffer instead.

Then, when the script ends (or when you call ob_end_flush()

), the contents of that buffer are written to standard output.

Usage ob_gzhandler

converts the contents of the buffer before writing it to stdout so that it is compressed with gzip. (Browsers that support gzip compression reverse this at the opposite end by unpacking the contents.)



Ok, let me explain it like this:

This is just one of the uses for the buffer system, but I think it's cool.

First, I want you to take a look at this animation.

Operating system startup

If you have a php script that has a level based structure like this, for example, you can write:

The connection is established on the database server.

Selected database: my_database

Data request started

Data request completed (found: 200 rows)


etc .. but if you are not using output buffering and flushing, you will see these lines when all your script finishes executing. But when the thought is, "I want to see what my script is doing, when!", You first need to ...

Sorry, you first need to set implicit_flush to "on" in your php.ini file and restart your Apache server to see it all.

secondly, you need to open the output buffering (short for "ob") with "ob_start ();" and then, anywhere in your code, "echo", and then "ob_flush ();" commands to view your script in real time.

Later it is also used to buffer static file based content like this:

  • place ob_start () at the beginning of your page (or at the beginning of the content you want to capture)
  • place ob_end_flush () at the end of your page (or at the end of the content you want to capture);

  • then $ my_var = ob_get_contents (); to get all the HTML output that the server generates and sends to the client into my_var and then use it as you see fit. Basically it is stored in a file and by checking the file's last modified date is used as static buffering.

Hope I can light some onions.



Please visit the link. Hope you have a clear understanding of your question.



All Articles