What are the best practices for handling data transfer in PHP, specifically with $_GET?

When handling data transfer in PHP, specifically with $_GET, it is important to validate and sanitize the data to prevent security vulnerabilities such as SQL injection or XSS attacks. To do this, always use functions like filter_input() or htmlspecialchars() to sanitize the input data before using it in your application.

// Example of handling data transfer with $_GET in PHP
$user_id = filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT);

if ($user_id) {
    // Proceed with using the sanitized user_id
    echo "User ID: " . $user_id;
} else {
    // Handle invalid input or show an error message
    echo "Invalid user ID";
}