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?