What are some best practices for filtering and escaping user input in PHP forms, especially when using the GET method?

When dealing with user input in PHP forms, especially when using the GET method, it is important to filter and escape the input to prevent security vulnerabilities such as SQL injection and cross-site scripting (XSS) attacks. One common way to do this is by using the filter_input() function to sanitize the input data. Additionally, you can use functions like htmlspecialchars() to escape any special characters in the input.

// Filter and escape user input from a GET form submission
$input = filter_input(INPUT_GET, 'input_field', FILTER_SANITIZE_STRING);
$escaped_input = htmlspecialchars($input);

// Now you can safely use $escaped_input in your application
echo "User input: " . $escaped_input;