Skip to content
Advertisement

Create link in PHP with Get Parameters

This page is quite configurable with different parameters. So, it may be called like:

www.example.com/index.php?test=1&foo=3&bar=4

Now my question is, what is the idiom for generating a link to the current page but with one parameter changed?

For example, I’d like to change foo to 5, what’s the easiest way to generate a link like:

www.example.com/index.php?test=1&foo=5&bar=4

I can loop through and do it manually, but I figured there would be some common idiom for this.

Advertisement

Answer

array_merge() is what you’re looking for

<a href="http://www.example.com/index.php?<?php echo http_build_query(array_merge($_GET, array('foo' => 5)), '', '&amp;');?>">link text</a>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement