Take the 2-minute tour ×

I have a set of custom PHP scripts. They load a main PHP file (to include other files, set settings, etc.) From this script I'd like to insert a callback function that is called by the scripts that included the main file just before ending.

What I intend to do is make a simple custom output cache with ob_get_contents(). I want to do ob_start() in main.php, then have the callback function store the output.

share|improve this question
Please rephrase this as a question. –  Asaph Sep 29 '09 at 3:35
add comment

2 Answers

up vote 1 down vote accepted

ob_start() can do what you want:


function store_output($output) {
  // do something with $output
  return $output;

When the output buffer is flushed, the function will be called. Output buffers can be nested.

share|improve this answer
What would happen if you called ie. ob_get_clean()? –  Matthew Scharley Sep 29 '09 at 3:42
It would call the callback. Is that desired behaviour? –  cletus Sep 29 '09 at 3:45
Oops, I should have looked at the PHP documentation first :). That's exactly what I need! –  heyitsme Sep 29 '09 at 3:57
I don't know, I'm not the OP! I was just curious. –  Matthew Scharley Sep 29 '09 at 4:57
add comment

See register_shutdown_function:

Register a function for execution on shutdown

Example from the manual:

function shutdown()
    // This is our shutdown function, in 
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;

share|improve this answer
add comment

Your Answer


