How should data passed through $_GET be validated or filtered based on the context in which it will be used?

Data passed through $_GET should be validated and filtered based on the context in which it will be used to prevent security vulnerabilities such as SQL injection or XSS attacks. It is important to sanitize the input data to ensure that only expected and safe values are used in the application.

// Example of validating and filtering data passed through $_GET
$id = isset($_GET['id']) ? intval($_GET['id']) : 0; // Validate and sanitize integer input
$name = isset($_GET['name']) ? filter_var($_GET['name'], FILTER_SANITIZE_STRING) : ''; // Sanitize string input

// Now $id and $name can be safely used in the application