Why is it recommended to place session_start() before any HTML code in PHP?

Placing session_start() before any HTML code in PHP is recommended because session_start() must be called before any output is sent to the browser. This is because session_start() sends headers to the browser to set up sessions, and headers must be sent before any other content. If session_start() is placed after HTML code, it may result in headers already being sent error.

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Session Example</title>
</head>
<body>
    <?php
    // PHP code here
    ?>
</body>
</html>