PHP ob_get_contents() Function

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

Definition

In PHP, ob_get_contents() returns a copy of the active output buffer without clearing it and without turning buffering off.

Use it to peek at what has been echoed so far, check a length, or build a string while the buffer keeps collecting more output.

Syntax

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

PHP
$copy = ob_get_contents();
CallWhat it does
ob_get_contents()Returns the current buffer text, or false if none.
ob_start(); echo "a"; $c = ob_get_contents();$c is "a" and the buffer still holds "a".
strlen(ob_get_contents())Reads the length of the buffered string without ending the buffer.

The buffer keeps its contents after the call. To get and clear/end, use ob_get_clean(). To get and flush/end, use ob_get_flush().

Library declarations
PHP
ob_get_contents(): string|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
string|falseA copy of the buffer contents on success, or false when no buffering is active.

PHP Examples

Example 1 — Peek Without Clearing

Copy the buffer twice to show that the text is still there after the first read.

PHP
<?php
ob_start();
echo "sample";
$first = ob_get_contents();
$second = ob_get_contents();
ob_end_clean();
echo $first, " / ", $second, PHP_EOL;

How It Works

  1. "sample" is written into the buffer.
  2. The first ob_get_contents() copies "sample".
  3. The second call copies "sample" again because nothing was cleared.
  4. ob_end_clean() discards the buffer; we print both copies.

Example 2 — Contents Grow With More Echo

Read the buffer, echo more, then read again to see the growing string.

PHP
<?php
ob_start();
echo "one";
$a = ob_get_contents();
echo "-two";
$b = ob_get_contents();
ob_end_clean();
echo $a, " then ", $b, PHP_EOL;

How It Works

  1. After the first echo, the copy is "one".
  2. Another echo appends "-two" inside the same buffer.
  3. The second copy is "one-two".
  4. ob_end_clean() ends buffering; both snapshots are printed.

Example 3 — Compare With Length

Use ob_get_contents() and ob_get_length() together for a quick check.

PHP
<?php
ob_start();
echo "PHP";
$copy = ob_get_contents();
$len = ob_get_length();
ob_end_clean();
echo var_export($copy, true), " length=", $len, PHP_EOL;

How It Works

  1. Three characters are buffered.
  2. ob_get_contents() returns the string "PHP".
  3. ob_get_length() returns 3, matching strlen of that string.
  4. ob_end_clean() clears and ends; we print both values.

Frequently Asked Questions

It returns a copy of the active output buffer without clearing or ending that buffer.
No. The text stays until you clean, flush, or end the buffer with another function.
When there is no active output buffer. Always check the result if buffering might not be on.
Use ob_get_clean() if you want to discard after reading, or ob_get_flush() if you want to send after reading.
No. You get a string copy. Changing the variable does not change what remains inside the buffer.

Next: ob_get_flush()

ob_get_flush() returns the contents of the active output buffer, flushes (sends) that buff.

ob_get_flush() 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