I want to add GET parameters to URLs that may and may not contain GET parameters without repeating ?
or &
.
Example:
If I want to add category=action
$url="http://www.acme.com";
// will add ?category=action at the end
$url="http://www.acme.com/movies?sort=popular";
// will add &category=action at the end
If you notice I'm trying to not repeat the question mark if it's found.
The URL is just a string.
What is a reliable way to append a specific GET parameter?
Example with updating existent parameters.
Also url_encode used, and possibility to don't specify parameter value
another improved function version. Mix of existing answers with small improvements (port support) and bugfixes (checking keys properly).