What are the benefits of using templates in PHP to separate HTML code from PHP logic?

Using templates in PHP helps to separate the presentation layer (HTML) from the business logic (PHP), making the code more organized and easier to maintain. Templates allow for reusability of code, as the same template can be used across multiple pages with different data. This separation also allows designers and developers to work independently on their respective parts of the codebase.

<?php
// Template file (template.php)
<html>
<head>
    <title><?php echo $title; ?></title>
</head>
<body>
    <h1>Welcome, <?php echo $username; ?>!</h1>
</body>
</html>

// PHP file (index.php)
<?php
$title = "Homepage";
$username = "JohnDoe";
include 'template.php';
?>