How do I print out all the parameters and their value from a URL without using e.g. print $_GET['paramater-goes-here'];
multiple times?
8 Answers
I use
print_r($_GET);
-
1such a lovely and simple answer. saved my time. #SF great use of opensource knowledge. Jul 16, 2013 at 3:57
foreach($_GET as $key => $value){
echo $key . " : " . $value . "<br />\r\n";
}
-
2This answer's great, because while print_r is quicker, this allows you to use $key and $value in your code. Nov 25, 2014 at 14:39
The parameters are in the URL, so are available in $_GET
; and you can loop over that array using foreach
:
foreach ($_GET as $name => $value) {
echo $name . ' : ' . $value . '<br />';
}
Its easy to get all request parameters from url.
<?php
print_r($_REQUEST);
?>
This will return an array format.
You can also use parse_url()
and parse_str()
:
$url = 'http://www.example.com/index.php?a=1&b=2&c=3&d=some%20string';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query);
parse_str($query, $arr);
echo $query; // a=1&b=2&c=3&d=some%20string
echo $a; // 1
echo $b; // 2
echo $c; // 3
echo $d; // some string
foreach ($arr as $key => $val) {
echo $key . ' => ' . $val . ', '; // a => 1, b => 2, c => 3, d => 4
}
-
1Actually this a nice answer because you can parse a string url outside of a request Oct 8, 2015 at 9:22
Try this.....
function get_all_get()
{
$output = "?";
$firstRun = true;
foreach($_GET as $key=>$val) {
if(!$firstRun) {
$output .= "&";
} else {
$firstRun = false;
}
$output .= $key."=".$val;
}
return $output;
}
-
2Thank you for posting this, this little function you wrote saved me some time, albeit I had to rework it for more of a functional use, but nonetheless what I needed. Didn't think about going this route. Jun 23, 2015 at 16:51
-
-
i use:
$get = $_REQUEST;
$query_string = '?';
foreach ($get as $key => $value) {
$query_string .= $key . '=' . $value . '&';
}
$query_string;
-
1The question is about the parameters in the URL. The gets the data from the URL and the request body and the cookies. It's also vulnerable to XSS attacks.– QuentinJan 7, 2022 at 13:22
GET
andPOST
: insecureweb.com/web-security/http-methods-get-vs-post