4

How do you append something to the beginning of the output buffer?

For example, say you have the following code:

ob_start();

echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';

Before flushing the contents to the browser, how can I append something so that it appears before <p>Start of page.</p> when the page loads?

It sounds simple enough, like moving the pointer to the beginning of an array, but I couldn't find how to do it with the output buffer.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
ProgrammerGirl
  • 3,157
  • 7
  • 45
  • 82
  • I'm not sure why a question is tagged "PHP" and "pointers" at the same time... –  Nov 26 '12 at 19:09
  • @H2CO3: If it's confusing, please edit it out. I only added it because I mentioned moving the pointer in an array as an analogy. – ProgrammerGirl Nov 26 '12 at 19:11
  • 1
    The word you're looking for is "**prepend**". "Append" means "to add after the end" while "prepend" means "to add before the beginning". – WWW Nov 26 '12 at 19:15

5 Answers5

4

** PHP 5.3 **

ob_start(function($output) {
    $output = '<p>Prepended</p>'.$output;
    return $output;
});

echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';

** PHP < 5.3 **

function prependOutput($output) {
    $output = '<p>Appended</p>'.$output;
    return $output;
}

ob_start('prependOutput');

echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
Jason Brumwell
  • 3,482
  • 24
  • 16
  • 5
    I think you mean *prepend* :-) – cmbuckley Nov 26 '12 at 19:15
  • I'm confused about how this works. You wrote the code that does the prepending BEFORE the rest of the code, but could you please provide an example where you do the prepending with code that appears AFTER the rest of the "page" code (e.g. Start, middle, end of page)? – ProgrammerGirl Nov 26 '12 at 19:22
  • To rephrase: Will this execute the function `appendOutput` when `ob_start` is called, or at the very end just before the buffer is flushed? This is very important because I need the code that will be inside that function to be executed at the very end of the page, and not at the beginning. – ProgrammerGirl Nov 26 '12 at 19:28
  • @Programmer Yes it will execute just prior to flushing to the browser as per the documentation for reference http://www.php.net/ob_start under the parameter output_callback – Jason Brumwell Nov 26 '12 at 19:40
0

Use 2 ob_start commands and ob_end_flush() after what you want to first display then end the buffer with ob_end_flush again when you want to output the rest of the page.

eg:

ob_start();
ob_start();

echo '<p>Start of page.</p>';

ob_end_flush();

echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';

ob_end_flush();
ABC
  • 718
  • 8
  • 23
0

See the first parameter of ob_start (documentation here), it allows you to provide a callback to be called when the buffer is flushed or cleaned. It receives a string as a parameter and outputs a string, therefore it should be easy to

function writeCallback($buffer)
{
    return "Added before " . $buffer;
}

ob_start("writeCallback");
emartel
  • 7,712
  • 1
  • 30
  • 58
  • Will this execute the function `writeCallback` when `ob_start` is called, or at the very end just before the buffer is flushed? This is very important because I need the code that will be inside that function to be executed at the very end, and not at the beginning. – ProgrammerGirl Nov 26 '12 at 19:26
  • At the very end, you will have the full buffer! – emartel Nov 26 '12 at 20:01
0

You can get content of buffer using ob_get_contents() function

ob_start();
echo "World! ";
$out1 = ob_get_contents();
echo "Hello, ".$out1;
Alex
  • 1,522
  • 1
  • 15
  • 17
  • This solution is easy enough to understand, but does flushing the entire contents to a variable end up using a lot more RAM, or is this not a concern? I ask because I will be using this in quite a few pages of the site and in one of the most active areas, so I want to make sure that throwing everything into a variable doesn't suddenly double the amount of RAM being used. – ProgrammerGirl Nov 26 '12 at 19:24
  • If you want clean current buffer to reduce memory usage you can call to $out1 = ob_get_flush(); – Alex Nov 27 '12 at 08:51
0

Are you wanting it before any output at all? If so, then you're looking for the auto_prepend_file directive. http://php.net/manual/en/ini.core.php

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • Correct, before any output to the browser, but could you please provide an example of how this would work? – ProgrammerGirl Nov 26 '12 at 19:22
  • You create a config variable in your php.ini file the specifies `auto_prepend_file=/path/to/whatever.php`. That `whatever.php` is processed before any processing on a given page. See the docs on the page I linked to. – Andy Lester Nov 26 '12 at 20:02