What are the best practices for handling user input in PHP to prevent syntax errors and potential security vulnerabilities?

When handling user input in PHP, it is crucial to sanitize and validate the input to prevent syntax errors and potential security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using PHP's filter_input() function to sanitize input data based on the type of input expected.

$user_input = filter_input(INPUT_POST, 'user_input', FILTER_SANITIZE_STRING);