What are some basic PHP functions that can be used to handle user input in a web application?

When handling user input in a web application, it is important to validate and sanitize the data to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. PHP offers several built-in functions to help with this, such as filter_input() for validating input from external sources, htmlentities() for escaping HTML characters, and stripslashes() for removing backslashes.

// Validate and sanitize user input using filter_input()
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

// Escape HTML characters using htmlentities()
$comment = htmlentities($_POST['comment']);

// Remove backslashes using stripslashes()
$password = stripslashes($_POST['password']);