Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Adding parameters to a url?

Tags:

php

If I have the url mysite.com/test.php?id=1. The id is set when the page loads and can be anything. There could also be others in there such as ?id=1&sort=new. Is there a way just to add another to the end without finding out what the others are first then building a new url? thanks.

like image 728
Andy Lobel Avatar asked Nov 28 '22 03:11

Andy Lobel


1 Answers

As an alternative to Kolink's answer, I think I would utilize http_build_query(). This way, if there is nothing in the query string, you don't get an extra &. Although, it won't really make a difference at all. Kolink's answer is perfectly fine. I'm posting this mainly to introduce you to http_build_query(), as you will likely need it later:

http_build_query(array_merge($_GET, array('newvar'=>'123')))

Basically, we use http_build_query() to take everything in $_GET, and merge it with an array of any other parameters we want. In this example, I just create an array on the fly, using your example parameter. In practice, you'll likely have an array like this somewhere already.

like image 124
Brad Avatar answered Dec 05 '22 22:12

Brad