PHP ob_get_flush() Function

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

Definition

In PHP, ob_get_flush() returns the contents of the active output buffer, flushes (sends) that buffer, and turns it off.

It combines reading the string with ob_end_flush(). Use it when you need the text in a variable and also want that same text sent onward as the buffer ends.

Syntax

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

PHP
$sent = ob_get_flush();
CallWhat it does
ob_get_flush()Returns the buffer string, flushes it, and ends the buffer.
ob_start(); echo "x"; $s = ob_get_flush();$s is "x" and "x" is also sent as the buffer ends.
ob_get_level()Drops by one after a successful ob_get_flush().

With nested buffers, the flush sends into the next outer buffer. The returned string is still the inner contents. On failure, the function returns false.

Library declarations
PHP
ob_get_flush(): string|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

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

PHP Examples

Example 1 — Get the String and Flush

Capture and flush in one step, then print a label that includes the returned string.

PHP
<?php
ob_start();
echo "payload";
$s = ob_get_flush();
echo PHP_EOL, "returned=", $s, PHP_EOL;

How It Works

  1. "payload" is buffered.
  2. ob_get_flush() returns "payload", sends it, and ends the buffer.
  3. You see "payload" from the flush itself.
  4. The next echo prints returned=payload using the variable.

Example 2 — Inner Get-Flush Into an Outer Buffer

Flush an inner buffer into an outer one, keep the returned inner string, then clean the outer buffer.

PHP
<?php
ob_start();
echo "OUT-";
ob_start();
echo "IN";
$inner = ob_get_flush();
$outer = ob_get_clean();
echo "inner=", $inner, " outer=", $outer, PHP_EOL;

How It Works

  1. Outer holds "OUT-"; inner holds "IN".
  2. ob_get_flush() returns "IN" and sends "IN" into the outer buffer.
  3. The outer buffer is now "OUT-IN".
  4. ob_get_clean() returns that outer string without printing it again during capture.

Example 3 — Level After Get Flush

Confirm that ob_get_flush() lowers the nesting level after it returns the string.

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

How It Works

  1. Level is 1 while "z" is buffered.
  2. ob_get_flush() sends "z", returns "z", and ends the buffer.
  3. Level becomes 0.
  4. We print the returned text and the level change on a new line.

Frequently Asked Questions

It returns the active buffer as a string, flushes (sends) that buffer, and turns the buffer off.
Both return the string and end the buffer. ob_get_flush() sends the contents; ob_get_clean() discards them.
ob_end_flush() only returns true or false. ob_get_flush() also gives you the buffer string.
You see it once from the flush, and again only if your code prints the returned string. Printing the variable is optional.
The inner flush sends into the outer buffer. The function still returns the inner string to your variable.

Next: ob_get_length()

ob_get_length() returns the length in bytes of the contents currently stored in the active.

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