What are the best practices for integrating PHP code into template files in PHP development?

When integrating PHP code into template files in PHP development, it is important to separate the PHP logic from the HTML markup for better readability and maintainability. One common practice is to use PHP opening and closing tags within the HTML code to insert dynamic content or execute PHP functions. It is also recommended to avoid using inline PHP code excessively and instead, utilize functions or include files for reusable code blocks.

<!-- Template file example -->
<html>
<head>
    <title><?php echo $pageTitle; ?></title>
</head>
<body>
    <h1>Welcome, <?php echo getUsername(); ?>!</h1>
    <p><?php include 'footer.php'; ?></p>
</body>
</html>