Output buffering vs. storing content into variable in PHP
Asked Answered
I

2

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?

Intelsat answered 26/1, 2013 at 15:51 Comment(0)
C
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.

[...]

Chlorate answered 26/1, 2013 at 15:53 Comment(5)
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.)Trstram
Ok, I understand. What about from the memory perspective. Which is better/worst? Should I buffer whole page or parts of it?Intelsat
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.Chlorate
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... ?Intelsat
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...Chlorate
V
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

Vortumnus answered 26/1, 2013 at 15:55 Comment(4)
Good point. Anyway, why should someone compress content with PHP if you can do it with web server (apache, nginx) easily?Intelsat
@BorutTomazin Compression is just one of the things you can accomplish with output buffering; you can do text rewriting as well.Gretta
I dont use output buffer functions, as it sometimes conflicts with other parts of the applications (have faced that in wordpress). I prefer: https://mcmap.net/q/478695/-html-into-php-variable-html-outside-php-codeMarshmallow
@Marshmallow 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.Gretta

© 2022 - 2024 — McMap. All rights reserved.