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 *

nine − two =

This site uses Akismet to reduce spam. Learn how your comment data is processed.