How can one streamline the process of collecting and appending $_GET values in PHP navigation?

When navigating between pages in a PHP application, it can be useful to collect and append $_GET values to maintain state across different pages. One way to streamline this process is by creating a function that automatically adds the current $_GET values to any links within the navigation. This can be achieved by looping through the $_GET values and appending them to the links using the http_build_query function.

function appendGetParamsToLinks($link) {
    $queryParams = http_build_query($_GET);
    $link .= (parse_url($link, PHP_URL_QUERY) ? '&' : '?') . $queryParams;
    return $link;
}

// Example usage
echo '<a href="' . appendGetParamsToLinks('page.php') . '">Page</a>';