What best practices should be followed when structuring PHP code to ensure that headers are not sent prematurely, and how can developers avoid common pitfalls such as whitespace or encoding issues that may trigger errors like "Cannot send session cache limiter"?

To ensure that headers are not sent prematurely in PHP code, developers should avoid outputting any content (including whitespace) before calling the `session_start()` function. Additionally, developers should check for any encoding issues that may trigger errors like "Cannot send session cache limiter" by ensuring that the PHP file is saved in UTF-8 encoding without a BOM.

<?php
ob_start(); // Start output buffering
session_start(); // Start the session

// Your PHP code here

ob_end_flush(); // Flush the output buffer
?>