Max
Max

Reputation: 13334

Stop and return output buffering at the same time

Once I have started output buffering with ob_start(), I would like, in one command, to stop the output buffering and get its content (without sending it to the output). I reviewed the available functions, and my understanding of each function is as follows:

                 clear  return  send    stop
ob_clean          x         
ob_end_clean      x                      x
ob_end_flush                      x      x
ob_flush                          x 
ob_get_clean      x        x             x  // should be called ob_get_end_clean
ob_get_contents            x        
ob_get_flush               x      x 

As far as I can see, there isn't a function which returns the output after stopping the buffering, so when I want to capture the output of a function I have to do it in 3 steps:

$output = ob_get_contents();
ob_end_clean();
return $output;

Am I missing something or is there a command to stop and return the output buffer in one go?

Upvotes: 2

Views: 286

Answers (2)

hakre
hakre

Reputation: 197554

To say it with the manual:

ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

(highlight by me). Comparing with your code:

$output = ob_get_contents();
ob_end_clean();

Your listing must have missed something.

Upvotes: 2

Pelshoff
Pelshoff

Reputation: 1464

This should be the one, it even mentions it in the description :)

http://www.php.net/manual/en/function.ob-get-clean.php

Upvotes: 1

Related Questions