What are some best practices for formatting PHP code, especially when interacting with HTML elements?

When formatting PHP code, especially when interacting with HTML elements, it is important to maintain readability and consistency for easier debugging and maintenance. One common best practice is to separate PHP logic from HTML markup by using PHP opening and closing tags within the HTML code. Additionally, using indentation and proper spacing can greatly improve code readability.

<?php
    $name = "John";
    $age = 30;
?>

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