What are some best practices for structuring PHP code to ensure proper execution and prevent common errors like header modification issues?

Header modification issues can occur when attempting to modify headers after they have already been sent to the browser. To prevent this, it is best practice to structure your PHP code in a way that ensures headers are only modified before any content is sent to the browser. One way to achieve this is by placing all header modifications at the beginning of your PHP script, before any HTML or output is generated.

<?php
// Set headers before any output
header("Content-Type: text/html");

// Start session before any output
session_start();

// Other PHP code goes here
?>