What are the best practices for writing PHP scripts to properly handle and process user inputs?

When writing PHP scripts to handle user inputs, it is important to sanitize and validate the data to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. Use functions like `htmlspecialchars()` to escape special characters and `filter_var()` to validate input data. Additionally, always use prepared statements when interacting with a database to prevent SQL injection.

// Sanitize user input
$user_input = htmlspecialchars($_POST['user_input']);

// Validate user input
if (filter_var($user_input, FILTER_VALIDATE_EMAIL)) {
    // Process the input
    echo "Valid email address: " . $user_input;
} else {
    echo "Invalid email address";
}