What are some best practices for sanitizing user input in PHP to prevent issues with special characters?

When dealing with user input in PHP, it is crucial to sanitize the data to prevent issues with special characters that could potentially lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. One common method to sanitize user input is to use the `htmlspecialchars()` function, which converts special characters to their HTML entities. This helps to ensure that any potentially harmful characters are rendered harmless when displayed on a webpage.

// Sanitize user input using htmlspecialchars()
$userInput = $_POST['user_input'];
$sanitizedInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');