0

I have set things up so my code reads the text of each page and automatically generates a table of contents and list of footnotes from it. I put footnote text into the page text in the place that it refers to. When the code reads the page file, it constructs a table of contents from the headers and puts that at the beginning of the page. It also removes the text of the footnotes and puts them in a list that it puts at the end of the page. It also manages numbering of the footnotes and placement of linked, superscripted footnote numbers for them.

To do this, instead of echo()ing page content, I compile the content into a variable, $PageText. I then have functions that read that variable, remove the footnote text, compile the table of contents and list of footnotes, and insert those into $PageText. I then echo($PageText);.

It seems that what I'm doing here is a homemade form of buffering, and that I could accomplish the same thing by something like

ob_start();
echo(<page contents>);
$PageText = ob_get_contents();
make_ToC($PageText);
make_footnotes($PageText);
ob_end_flush();

Would that be more efficient that using my own buffer variable? Or is there any other reason that it would better to use the built-in buffering system?

The reason I prefer to use my own buffer is that I have complete control of it. I don't really know what PHP might decide to do with content between the time that I echo() it and the time that I flush it.

A similar question was asked five years ago, Output buffering vs. storing content into variable in PHP. It's interesting that the answers seem to say that I would have better control of the contents by using the "ob_" system, whereas I feel like I have better control by using my own variable. Is there something I don't understand about that?

NewSites
  • 1,402
  • 2
  • 11
  • 26
  • You will not achieve what you want with the `ob_` functions. That buffers the output, but you cannot change it afterwards as you think you do with your example. So use your string variable, and forget about the `ob_` functions. – KIKO Software Aug 08 '18 at 14:42
  • @KIKOSoftware you cannot change the buffer, but you can store it into variable, delete and then output modified string – Iłya Bursov Aug 08 '18 at 14:54
  • @KIKO Software Duh! You're so right! Modifying $PageText has no effect on the buffer, of course. I would have to clean the buffer and then echo() the modified $PageText, which means I would just be using echo() to build $PageText instead of string concatenation, which would be lame. But is there any drawback to the kind of system I describe? – NewSites Aug 08 '18 at 14:55

1 Answers1

1

Output buffering is a technique to put already generated output on hold and has a number of usages:

  • Make better use of network transmissions (e.g. avoid sending almost empty HTTP packages)
  • Apply transparent transformations (e.g. minify generated HTML or rewrite URLs)
  • Being able to cancel the output altogether (e.g. in case of error)
  • Capture the output of functions that print to stdout (e.g. var_dump())

In my experience, functions that print to stdout tend to be an annoyance, esp. if the only apparent reason to do so is not having to prepend echo, because they get in the way of your application design. You don't always want to send their output as-is and you don't always want to print the output in the exact spot of the response flow where the function call happens.

What you describe is basically the standard text manipulation you can expect in any program. While you can certainly emulate it with output buffering, and as opinionated as it can get, I think it feels like a hack. In your case, you aren't even doing linear output. There's no obvious benefit of printing to stdout at once, given that you still need to manipulate the output because it isn't ready yet.

Katie
  • 45,622
  • 19
  • 93
  • 125
Álvaro González
  • 142,137
  • 41
  • 261
  • 360