Add a set of querystring params to a URL Example cases:

Need to add x=1, y=2 and querystring variables to the following URLs:


http://www.foo.com/blah/blah?test=1&amp;abc=2
http://www.foo.com/blah




//-- usage
get_link(array('x' =&gt; 1, 'y' =&gt; 2, 'z' =&gt; 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];

    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];

    foreach ( $my_params as $param_key =&gt; $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }

    if ( empty( $querystring ) )
    {
        //-- generates foo.com/blah?x=1&amp;y=2&amp;z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&amp;b=2&amp;x=1&amp;y=2&amp;z=string if a=1&amp;b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&amp;' . $param_querystring;
    }

    return $link;
}
Your code is very spaced out. It doesn't need to be.
Consider using industry-standard style by using Egyptian braces for control structures like if and foreach.
Get rid of the $http_host variable; you never use it.
Consider generalising your function to accept any URL instead of pulling REQUEST_URI.
You're kind of reinventing the wheel; PHP has the built-in functions parse_str and http_build_query that you can use.
Do you intend to replace existing variables, or just to append them to the request?


Here's an example that takes advantage of the built-in functions, and intelligently merges in conflicts. (ideone)

/**
 *  Like array_merge, but will recursively merge array values.
 *
 *  @param array $a1
 *      The array to be merged into.
 *  @param array $a2
 *      The array to merge in. Overwrites $a1, when string keys conflict.
 *      Numeric keys will just be appended.
 *  @return array
 *      The array, post-merge.
 */
function merge_query_var_arrays($a1, $a2) {
    foreach ($a2 as $k2 =&gt; $v2)
        if (is_string($k2))
            $a1[$k2] = isset($a1[$k2]) &amp;&amp; is_array($v2) ? merge_query_var_arrays($a1[$k2], $v2) : $v2;
        else
            $a1[] = $v2;
    return $a1;
}

/**
 *  @param string $query_string
 *      The URL or query string to add to.
 *  @param string|array $vars_to_add
 *      Either a string in var=val&amp;[...] format, or an array.
 *  @return string
 *      The new query string. Duplicate vars are overwritten.
 */
function add_query_vars($query_string, $vars_to_add) {
    if (is_string($vars_to_add))
        parse_str($vars_to_add, $vars_to_add);
    if (preg_match('/.*\?/', $query_string, $match))
        $query_string = preg_replace('/.*\?/', '', $query_string);
    parse_str($query_string, $query_vars);

    $query_vars = merge_query_var_arrays($query_vars, $vars_to_add);
    return @$match[0] . http_build_query($query_vars);
}