PHP ob_end_clean() Function

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

Definition

In PHP, ob_end_clean() erases the contents of the active output buffer and turns that top buffer off.

Nothing from that buffer is sent. The nesting level from ob_get_level() goes down by one. Use it when you captured output only to discard it, or when an inner buffer should disappear without printing.

Syntax

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

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

With nested buffers, only the top (innermost) buffer is ended. Outer buffers stay active. With no active buffer, the call fails and returns false.

Library declarations
PHP
ob_end_clean(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Discard Buffered Output

Buffer a message, end-clean it, then print a short status. The buffered message never appears.

PHP
<?php
ob_start();
echo "secret draft";
ob_end_clean();
echo "discarded", PHP_EOL;

How It Works

  1. ob_start() begins capturing output.
  2. echo stores "secret draft" in the buffer.
  3. ob_end_clean() erases that text and turns the buffer off.
  4. Only the final echo runs with buffering off, so "discarded" prints.

Example 2 — End Only the Inner Buffer

Start two buffers, end-clean the inner one, then get the outer buffer contents.

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

How It Works

  1. The outer buffer receives "outer-".
  2. A second ob_start() opens an inner buffer that receives "inner".
  3. ob_end_clean() discards "inner" and closes only the inner buffer.
  4. ob_get_clean() returns "outer-" from the remaining buffer and ends it.

Example 3 — Level Falls After End Clean

Print the nesting level before and after ob_end_clean().

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

How It Works

  1. One call to ob_start() makes the level 1.
  2. ob_end_clean() clears and removes that buffer.
  3. ob_get_level() becomes 0.
  4. The printed line shows the drop from 1 to 0.

Frequently Asked Questions

It erases the active output buffer and turns that buffer off. The discarded text is not sent.
ob_clean() clears the text but keeps buffering on. ob_end_clean() clears and turns the top buffer off.
ob_end_clean() discards. ob_end_flush() sends the buffer contents (through the handler) and then turns the buffer off.
ob_get_clean() returns the buffer string, then cleans and ends the buffer. ob_end_clean() only cleans and ends; you do not get the string back.
Yes, once per active buffer. With nested buffers, call it once for each level you want to remove. Calling it with no buffer returns false.

Next: ob_end_flush()

ob_end_flush() sends the contents of the active output buffer and then turns that top buff.

ob_end_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