What are the best practices for separating PHP logic from HTML layout, following the E.V.A principle?

When separating PHP logic from HTML layout, it is important to follow the E.V.A principle, which stands for "Escaping, Variables, and Avoiding." This means properly escaping output to prevent cross-site scripting attacks, using variables to pass data between PHP and HTML, and avoiding mixing PHP logic with HTML markup.

<?php
// PHP logic
$name = "John Doe";
$age = 30;

// HTML layout
?>
<!DOCTYPE html>
<html>
<head>
    <title>User Profile</title>
</head>
<body>
    <h1>Welcome, <?php echo htmlspecialchars($name); ?>!</h1>
    <p>You are <?php echo $age; ?> years old.</p>
</body>
</html>