PHP ob_end_flush() Function

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

Definition

In PHP, ob_end_flush() sends the contents of the active output buffer and then turns that top buffer off.

After a successful call, the nesting level drops by one. Use it when you are finished buffering and want the stored output to go out. To send but keep buffering, use ob_flush() instead.

Syntax

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

PHP
$ok = ob_end_flush();
CallWhat it does
ob_end_flush()Flushes and ends the active buffer; returns true on success.
ob_start(); echo "hi"; ob_end_flush();Sends "hi" and turns buffering off.
ob_get_level()Drops by one after a successful ob_end_flush().

With nested buffers, the flushed text goes to the next outer buffer (or to the real output if this was the last buffer). ob_end_flush() does not return the string; use ob_get_flush() when you need both the text and a flush-and-end.

Library declarations
PHP
ob_end_flush(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Flush and End a Buffer

Buffer a greeting, then ob_end_flush() so the greeting prints and buffering stops.

PHP
<?php
ob_start();
echo "hello";
ob_end_flush();
echo PHP_EOL, "done", PHP_EOL;

How It Works

  1. ob_start() captures output.
  2. echo stores "hello" in the buffer.
  3. ob_end_flush() sends "hello" and turns the buffer off.
  4. The following echo prints a newline and "done" with no buffering.

Example 2 — Inner Flush Feeds the Outer Buffer

Flush-and-end an inner buffer so its text lands in the outer buffer, then capture the outer text.

PHP
<?php
ob_start();
echo "A";
ob_start();
echo "B";
ob_end_flush();
$out = ob_get_clean();
echo $out, PHP_EOL;

How It Works

  1. The outer buffer gets "A".
  2. The inner buffer gets "B".
  3. ob_end_flush() sends "B" into the outer buffer and closes the inner one.
  4. ob_get_clean() returns "AB" from the outer buffer.

Example 3 — Level After End Flush

Show that ob_end_flush() lowers the nesting level like other end_* functions.

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

How It Works

  1. A buffer at level 1 holds "x".
  2. ob_end_flush() sends "x" and removes that buffer.
  3. The level becomes 0.
  4. We print the level change on the next line.

Frequently Asked Questions

It sends the active output buffer contents and turns that buffer off, lowering the nesting level by one.
ob_flush() sends the buffer and keeps buffering on. ob_end_flush() sends and then turns the top buffer off.
ob_end_flush() sends the text. ob_end_clean() throws the text away. Both turn the top buffer off.
No. It returns true or false. To get the string while flushing and ending, use ob_get_flush().
Flushing the inner buffer sends its contents to the next outer buffer. Only when the last buffer ends does the text go to the real output.

Next: ob_flush()

ob_flush() sends the contents of the active output buffer and keeps that buffer turned on.

ob_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