In the provided PHP code for a form submission, what are some best practices for sanitizing user input to prevent security vulnerabilities or errors in the application?

To prevent security vulnerabilities or errors in the application, it is important to sanitize user input before processing it. This can be done by using functions like htmlspecialchars() to convert special characters to HTML entities, or by using filter_input() with appropriate filters to validate and sanitize input data.

// Sanitize user input before processing
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';
$email = isset($_POST['email']) ? filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL) : '';
$message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '';