How can data passed via GET requests in PHP be validated to ensure data integrity and security?

When data is passed via GET requests in PHP, it is important to validate the data to ensure its integrity and security. This can be done by sanitizing the input data to remove any potentially harmful characters or code, validating the input against expected formats or ranges, and using PHP functions like filter_input() to validate specific types of data.

// Example of validating data passed via GET request in PHP
$user_id = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);

if ($user_id === false) {
    // Handle invalid input
    echo "Invalid user ID";
} else {
    // Proceed with using the validated user ID
    echo "User ID: " . $user_id;
}