PHP ob_get_clean() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Output Control

Definition

In PHP, ob_get_clean() returns the contents of the active output buffer, then cleans that buffer and turns it off.

It is the handy combo of "read the text" plus ob_end_clean(). Beginners use it to capture HTML or messages into a string, edit the string, and print later.

Syntax

Call ob_get_clean() with the values you need. Start with this line.

PHP
$html = ob_get_clean();
CallWhat it does
ob_get_clean()Returns the buffer string and ends the buffer, or false on failure.
ob_start(); echo "hi"; $s = ob_get_clean();Stores "hi" in $s; buffering is off afterward.
ob_get_level()Drops by one after a successful ob_get_clean().

On failure (no suitable active buffer), it returns false. Unlike a plain get, the buffer is gone afterward, so a second call needs a new ob_start().

Library declarations
PHP
ob_get_clean(): string|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
string|falseThe buffer contents as a string on success, or false on failure.

PHP Examples

Example 1 — Capture Echo Into a Variable

Buffer a short message, pull it into a variable with ob_get_clean(), then print a modified version.

PHP
<?php
ob_start();
echo "Hello World";
$out = ob_get_clean();
echo strtoupper($out), PHP_EOL;

How It Works

  1. ob_start() begins capturing.
  2. echo stores "Hello World" in the buffer.
  3. ob_get_clean() returns that string and turns buffering off.
  4. strtoupper() changes the copy, and we print HELLO WORLD.

Example 2 — Level Returns to Zero

Show the nesting level before capture and after ob_get_clean().

PHP
<?php
ob_start();
echo "data";
$before = ob_get_level();
$text = ob_get_clean();
$after = ob_get_level();
echo $text, " | level ", $before, "->", $after, PHP_EOL;

How It Works

  1. Buffering is active at level 1.
  2. ob_get_clean() returns "data" and ends the buffer.
  3. The level becomes 0.
  4. One echo prints the text and the level change.

Example 3 — Nested Capture Keeps the Outer Buffer

Get-clean the inner buffer into a variable while the outer buffer still holds its own text.

PHP
<?php
ob_start();
echo "outer:";
ob_start();
echo "inner";
$inner = ob_get_clean();
$outer = ob_get_clean();
echo $outer, $inner, PHP_EOL;

How It Works

  1. The outer buffer stores "outer:".
  2. The inner buffer stores "inner".
  3. First ob_get_clean() returns "inner" and closes only the inner buffer.
  4. Second ob_get_clean() returns "outer:" and closes the outer buffer; we print them together.

Frequently Asked Questions

It returns the active output buffer as a string, then cleans and turns that buffer off.
ob_get_contents() copies the text and leaves buffering on. ob_get_clean() returns the text and ends the buffer.
ob_get_clean() discards after reading (like end_clean). ob_get_flush() sends after reading (like end_flush). Both return the string and end the buffer.
false, for example when there is no active output buffer.
Only if you still have another active buffer (nesting), or after you call ob_start() again. With no buffer, the second call returns false.

Next: ob_get_contents()

ob_get_contents() returns a copy of the active output buffer without clearing it and witho.

ob_get_contents() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful