What are some best practices for validating and sanitizing user input in PHP to prevent bypassing restrictions?

To prevent bypassing restrictions, it is essential to validate and sanitize user input in PHP. This can be achieved by using functions like filter_input() to validate input data against a specified filter, and functions like htmlentities() or htmlspecialchars() to sanitize input data by converting potentially harmful characters into their HTML entities.

// Validate and sanitize user input
$user_input = filter_input(INPUT_POST, 'user_input', FILTER_SANITIZE_STRING);

// Output the sanitized input
echo htmlentities($user_input);