What are best practices for securely passing and handling user input in PHP scripts to prevent potential vulnerabilities?

When handling user input in PHP scripts, it is crucial to sanitize and validate the data to prevent potential vulnerabilities such as SQL injection or cross-site scripting attacks. One of the best practices is to use functions like htmlspecialchars() to escape special characters and prevent malicious code execution.

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