What are some potential pitfalls of determining the Content-Length in PHP, especially when dealing with dynamic content?

When determining the Content-Length in PHP for dynamic content, one potential pitfall is inaccurate content length calculation due to output buffering or encoding issues. To ensure an accurate Content-Length header, you can use the ob_start() function to buffer the output, calculate the content length, and then send the header before outputting the buffered content.

<?php
ob_start();

// Output your dynamic content here

$content = ob_get_clean();
$contentLength = strlen($content);

header('Content-Length: ' . $contentLength);

echo $content;
?>