Example 1 — Peek Without Clearing
Copy the buffer twice to show that the text is still there after the first read.
<?php
ob_start();
echo "sample";
$first = ob_get_contents();
$second = ob_get_contents();
ob_end_clean();
echo $first, " / ", $second, PHP_EOL; How It Works
- "sample" is written into the buffer.
- The first ob_get_contents() copies "sample".
- The second call copies "sample" again because nothing was cleared.
- ob_end_clean() discards the buffer; we print both copies.

