What best practices should be followed when combining PHP and HTML to ensure proper functionality and security on a website?

When combining PHP and HTML on a website, it is important to properly separate the PHP code from the HTML code to maintain readability and organization. To ensure proper functionality and security, it is recommended to use PHP to handle server-side processing and data manipulation, while HTML is used for presenting content to the user. Additionally, it is crucial to sanitize user input to prevent SQL injection and cross-site scripting attacks.

<?php
// Sanitize user input to prevent SQL injection and cross-site scripting attacks
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);

// Perform server-side processing and data manipulation
// Example: Check if the username and password match a record in the database
if ($username === 'admin' && $password === 'password') {
    echo 'Login successful!';
} else {
    echo 'Invalid username or password';
}
?>