What are some best practices for structuring HTML and PHP code in a web application?

One best practice for structuring HTML and PHP code in a web application is to separate the presentation layer (HTML) from the logic layer (PHP). This can be achieved by using templates or a templating engine to keep the HTML code separate from the PHP code, making the codebase more maintainable and easier to read.

// Example of separating HTML from PHP using templates

// HTML template file (template.html)
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title; ?></title>
</head>
<body>
    <h1><?php echo $heading; ?></h1>
    <p><?php echo $content; ?></p>
</body>
</html>

// PHP code file (index.php)
<?php
$title = "Welcome to my website";
$heading = "Hello, World!";
$content = "This is a simple example of separating HTML from PHP using templates.";

// Load the HTML template file
include 'template.html';
?>