What are some best practices for handling variables received via GET in PHP?

When receiving variables via GET in PHP, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One common best practice is to use PHP's filter_input() function to sanitize input and check for specific types of data. Additionally, always validate and sanitize input before using it in any database queries or outputting it to the browser.

// Example of handling variables received via GET in PHP
$userId = filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT);

if ($userId) {
    // Use the sanitized $userId in your code
    echo "User ID: " . $userId;
} else {
    // Handle invalid input
    echo "Invalid user ID";
}