PHP: Hypertext Preprocessor (original) (raw)

ob_get_clean

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

ob_get_clean — Get the contents of the active output buffer and turn it off

Parameters

This function has no parameters.

Return Values

Returns the contents of the active output buffer on success or [false](reserved.constants.php#constant.false) on failure.

Caution

ob_get_clean() will return false but will not generate an [E_NOTICE](errorfunc.constants.php#constant.e-notice) if there is no active output buffer.

Errors/Exceptions

If the function fails it generates an [E_NOTICE](errorfunc.constants.php#constant.e-notice).

Examples

Example #1 A simple ob_get_clean() example

`<?php

ob_start

();

echo

"Hello World";$out = ob_get_clean(); out=strtolower(out = strtolower(out=strtolower(out);var_dump($out);
?>`

The above example will output:

See Also

Found A Problem?

geo dot artemenko at gmail dot com

11 years ago

The definition should mention that the function also "turns off output buffering", not just cleans it.

steven at bielik dot com

14 years ago

`Also, don't forget that you will need to ob_start() again for any successive calls:

Output: 12

Without the second ob_start(), the output is 21 ...

`

paul+phpnet at earth2me dot com

11 years ago

`Keep in mind that output may be buffered by default, depending on how you are running PHP (CGI, CLI, etc.). You can use ob_get_level() to determine if an output buffer has already been started. On most web servers I've used, output buffering is already one level deep before my scripts start running.

You should only end as many output buffers as you start. Assuming that your buffer is always the first buffer, or otherwise closing pre-existing buffers, could lead to problems. In PHP 5.5, you can ensure that output buffers are ended properly using a try-finally block.

Something like this is almost guaranteed to break stuff:

1) { ob_end_flush(); }$content = ob_get_clean(); ?>

The problem is that number, "1". Using a fixed number there is asking for trouble. Instead, use ob_get_level() to get the number of output buffers applied when your code starts, and return to that number, if you really must use an unknown number of output buffers:

$start_ob_level) { ob_end_flush(); }// And now, the final output buffer that belongs to us: $content = ob_get_clean(); ?>

`