What are the recommended security measures to implement when handling user input in PHP?

When handling user input in PHP, it is crucial to implement security measures to prevent common vulnerabilities like SQL injection, cross-site scripting (XSS), and CSRF attacks. One way to enhance security is by using functions like htmlentities() or htmlspecialchars() to sanitize user input and prevent malicious code from being executed.

// Example of using htmlentities() to sanitize user input
$input = $_POST['user_input'];
$sanitized_input = htmlentities($input, ENT_QUOTES, 'UTF-8');