Example 1 — Replace Buffer Contents
Write one string, clean the buffer, write another, then capture what remains.
<?php
ob_start();
echo "first";
ob_clean();
echo "second";
$text = ob_get_clean();
echo $text, PHP_EOL; How It Works
- ob_start() begins buffering.
- "first" is written, then ob_clean() erases it.
- "second" is written into the same still-active buffer.
- ob_get_clean() returns "second", ends the buffer, and we print it.

