What are some best practices for handling and manipulating query strings in PHP to maintain clean and functional URLs?

When handling query strings in PHP, it is important to maintain clean and functional URLs for better user experience and SEO. One best practice is to sanitize and validate input to prevent security vulnerabilities and unexpected behavior. Another best practice is to use the `http_build_query()` function to construct query strings in a clean and readable way.

// Sanitize and validate input
$id = isset($_GET['id']) ? filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT) : null;

// Construct clean query string
$queryParams = [
    'page' => isset($_GET['page']) ? filter_var($_GET['page'], FILTER_SANITIZE_NUMBER_INT) : 1,
    'search' => isset($_GET['search']) ? filter_var($_GET['search'], FILTER_SANITIZE_STRING) : ''
];

$queryString = http_build_query($queryParams);

// Redirect with clean URL
header('Location: /example.php?' . $queryString);
exit;