What are best practices for organizing PHP and HTML code in a script?

When organizing PHP and HTML code in a script, it is best practice to separate the PHP logic from the HTML presentation. This can be achieved by using PHP to generate dynamic content and then embedding it within the HTML structure. This separation improves code readability, maintainability, and reusability.

<?php
// PHP logic to fetch data
$data = fetchDataFromDatabase();

// HTML structure with embedded PHP variables
?>
<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Content</title>
</head>
<body>
    <h1>Welcome, <?php echo $data['name']; ?></h1>
    <p>Your email is: <?php echo $data['email']; ?></p>
</body>
</html>