3

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

Is this true? How does the browser output data in this case, do I have to use ob_end_flush() to turn it off in the end?

Since ob_gzhandler compresses web pages, how do browsers handle these pages?

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

All help appreciated!

Eve
  • 193
  • 1
  • 6

2 Answers2

4

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

Then when the script finishes (or when you call ob_end_flush()), the contents of that buffer are written to stdout.

Using ob_gzhandler transforms the contents of the buffer before writing it to stdout, such that it is gzip compressed. (Browsers which support gzip compression reverse this on the opposite end, decompressing the content.)

Amber
  • 507,862
  • 82
  • 626
  • 550
1

Ok, let me explain it like this,

It is only one of the uses of the buffer system but I think it's kinda cool.

first I want you to look to this animation.

Operating System Start

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

Connection established to database server..

Database selected : my_database

Data query started

Data query ended (found:200 rows)

...

etc. but if you don't use output buffering and flushing, you will see these lines when all of your script execution ends. 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" at your php.ini file and restart your apache server to see all of this.

second, you need to open the output buffering (shorthand "ob") by "ob_start();", and then, place anywhere on your code "echo" statements and after that "ob_flush();" commands to see your script running on realtime.

Later, it is also used for file based static content buffering like this:

  1. place ob_start() at the start of your page (or the start of content you want to capture)

  2. place ob_end_flush() at the end of your page (or the end of content you want to capture);

  3. then $my_var = ob_get_contents(); to get all the HTML output that server creates and sends to the client into my_var variable and then use it as you want. Mostly it's saved to a file and by checking the file's last modification date, it's used as a static buffering.

I hope I could light some bulbs on your mind.

Community
  • 1
  • 1
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78