What Is Output Buffering

What is output buffering?

Output Buffering for Web Developers, a Beginner’s Guide:

Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script.

Advantages of output buffering for Web developers

  • Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it's not being sent to the browser in pieces as PHP processes the HTML.
  • All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable.
  • If you've ever encountered the message "Warning: Cannot modify header information - headers already sent by (output)" while setting cookies, you'll be happy to know that output buffering is your answer.

What is the difference between values 1 and 4096 of output_buffering in php.ini

output_buffering boolean/integer

You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096). This directive is always Off in PHP-CLI.

From the PHP Manual

What this means is :

  • A value of 0 is false/off so output buffering is disabled.
  • A value of 1 is true/on so that output buffering is enabled and has no set limit (at least in this scope) to the maxmum buffer size.
  • A value of >1 is the maximum number of bytes for a custom output buffer size.

Therefore, choices above in the PHP.ini config are:

    output_buffering= 0 / 1 / 1+

Output buffering vs. storing content into variable in PHP

The main differences:

1.) you can use "normal" output syntax, so for example an echo statement. You don't have to rewrite your problem.

2.) you have better control about the buffering, since buffers can be stacked. You don't have to know about naming conventions and the like, this makes implementations easier where the writing and using side are implemented separate from each other.

3.) no additional logic require to output buffered content, you just flush. Especially interesting if the output stream is something special. Why burden the controlling scope with dealing with that?

4.) you can use the same output implementation regardless of an output buffer has been created. THis is a question of transparency.

5.) you can 'catch' accidentially out bubbled stuff like warnings and the like and simply swallow it afterwards.

[...]

Output buffering not working or making any sense

ob_get_contents does not clear the buffer so when a script ends it is flushed to the output as usual.
If you only want to get data as string and clear buffer use ob_get_clean()

You can think of output buffering as creating "bookmarks" or "restore points" in output buffer. For example:

  • buffer is empty
  • echo 'hello' - output buffer has "hello" string
  • ob_start() - "bookmark" is created
  • echo 'world' - output buffer has two lines "hello" and "world" strings
  • ob_get_contents() - returns output buffer content since last "bookmark" - returns "world", but buffer contents stay the same
  • scripts ends and flushes whole output buffer to screen

  • if you use ob_get_clean() instead it returns contents since last "bookmark" and deletes it from output buffer - the buffer has only "hello" in it

EDIT: I know it's very simple and naive explanation but I recall it helped me a lot to grasp the concept.

output_buffering off at run time in php

By default output buffering value is set to "0".

  • Runtime Configuration: output_buffering
  • Nice Answer to "What is Output Buffering?"

PHP Output Buffering

Use ob_start() and ob_end_flush().

ob_start() at the start of the script before any output (not even an empty space).

When u want to output use ob_end_flush().



Related Topics



Leave a reply



Submit