How can the use of GET requests in PHP be optimized to prevent unintended code execution?

When using GET requests in PHP, it is important to sanitize and validate the input data to prevent unintended code execution or security vulnerabilities. One way to optimize the use of GET requests is to use filter_input() function to retrieve and filter input data from the GET array. This function allows you to specify the type of input data you are expecting, such as integer or string, and apply filters to sanitize the data.

$user_id = filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT);
if($user_id){
    // Proceed with using the sanitized user_id
} else {
    // Handle invalid input or display an error message
}