PHP function that adds a GET parameter to an URL

Here is a short and useful function, which you can use to add GET parameters to an URL. The function checks if the URL already contains GET parameters by looking for a “?” character. If the URL already contains GET parameters, it appends a & to the URL, and if it doesn’t contain any GET parameters, it appends “?” to the URL. After the connecting character has been inserted into the URL, the function appends the new GET parameter and its value, and returns the new URL.

function add_get_parameter_to_url($url, $name, $value)
{
	if (strpos($url, '?') !== FALSE) return $url."&".urlencode($name)."=".urlencode($value);
	else $url."?".urlencode($name)."=".urlencode($value);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close