6

I don't know exactly how output buffering works but as far as know it stores content into some internal variable.

Regarding this, what is the difference of not using output buffering and storing content in my own local variable instead and than echo it at the end of script?

Example with output buffering:

<?php
  ob_start();
  echo "html page goes here";
  ob_end_flush();
?>

And example without using output buffering:

<?php
  $html = "html page goes here";
  echo $html;
?>

What is the difference?

Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

2 Answers2

6

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.

[...]

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Another pro: you can capture the output of third party / native code that outputs content when you don't want it to, e.g. SoapServer. (sorry, that's kind of what #2 says.) – cmbuckley Jan 26 '13 at 15:56
  • Ok, I understand. What about from the memory perspective. Which is better/worst? Should I buffer whole page or parts of it? – Borut Tomazin Jan 26 '13 at 15:58
  • Typically the size of a markup page actually delivered is that small that it is uninteresting to think about it. During execution of a script it is different things that consume memory. So buffering or not and the strategy of _how_ to buffer is more a question of convenience: clean and transparent implementation should be the goal. – arkascha Jan 26 '13 at 15:59
  • What about when you have to load a lot of content on page like text fields from database which can contain huge amount of text... ? – Borut Tomazin Jan 26 '13 at 16:01
  • That would not be a 'typical' markup page then... I'd say only you can decide about a good buffering strategy, since only you know about the exact details... – arkascha Jan 26 '13 at 16:02
6

Output buffering gives you greater flexibility in separating the concerns of what to output, when to output and how to output without requiring any change to existing code.

You may have existing code that echoes their output instead of returning it; output buffering allows for that code to run without making any changes to it.

Besides the obvious ob_end_flush() you can also use $output = ob_get_contents() followed by ob_end_clean() to capture the output into a variable again. This allows you to write it into a file instead of displaying it on the screen.

Lastly, you can hook filters onto the output buffering system that enable compression on-the-fly.

See also: ob_gz_handler

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Good point. Anyway, why should someone compress content with PHP if you can do it with web server (apache, nginx) easily? – Borut Tomazin Jan 26 '13 at 16:09
  • @BorutTomazin Compression is just one of the things you can accomplish with output buffering; you can do text rewriting as well. – Ja͢ck Jan 26 '13 at 16:18
  • I dont use output buffer functions, as it sometimes conflicts with other parts of the applications (have faced that in wordpress). I prefer: https://stackoverflow.com/a/30821429/2377343 – T.Todua Sep 15 '18 at 16:19
  • @T.Todua and i prefer not using wordpress; how you use output buffering, and how it’s used in a system over which you have less control will ultimately determine which strategy to adopt. – Ja͢ck Sep 15 '18 at 20:02