What are the best practices for handling form submissions in PHP, especially when using GET method instead of POST?

When handling form submissions in PHP using the GET method, it is important to sanitize and validate the input data to prevent security vulnerabilities and ensure data integrity. One common practice is to use the filter_input function to retrieve and sanitize input values. Additionally, it is recommended to avoid directly outputting user input without proper escaping to prevent cross-site scripting attacks.

// Handle form submission using GET method
if ($_SERVER["REQUEST_METHOD"] == "GET") {
    // Sanitize and validate input data
    $name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
    
    // Output sanitized data
    echo "Name: " . htmlspecialchars($name) . "<br>";
    echo "Email: " . htmlspecialchars($email) . "<br>";
}