What is the best practice for adding additional GET parameters to a URL in PHP without overwriting existing parameters?

When adding additional GET parameters to a URL in PHP, it is important to ensure that existing parameters are not overwritten. One way to achieve this is by checking if the URL already contains parameters and then appending the new parameters accordingly. This can be done by using the `parse_url` function to parse the URL and then building the new URL with the additional parameters.

function add_url_parameters($url, $params) {
    $parsed_url = parse_url($url);
    $query = [];
    
    if(isset($parsed_url['query'])){
        parse_str($parsed_url['query'], $query);
    }
    
    $query = array_merge($query, $params);
    
    $parsed_url['query'] = http_build_query($query);
    
    $new_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . $parsed_url['query'];
    
    return $new_url;
}

// Example usage
$url = 'http://example.com/page.php?param1=value1';
$new_params = ['param2' => 'value2', 'param3' => 'value3'];
$new_url = add_url_parameters($url, $new_params);

echo $new_url;