Example 1 — Measure Growing Output
Echo in two steps and read the length after each step, then end-clean.
<?php
ob_start();
echo "Hello ";
$len1 = ob_get_length();
echo "World";
$len2 = ob_get_length();
ob_end_clean();
echo $len1, ", ", $len2, PHP_EOL; How It Works
- "Hello " is six characters, so the first length is 6.
- "World" is appended inside the same buffer.
- The second length is 11.
- ob_end_clean() discards the buffer; only the printed lengths remain.

